Solved

validation control for image size in asp.net with c#, and javascript

Posted on 2008-10-23
2
1,800 Views
Last Modified: 2012-05-05
Hi,
i am working on a module in which there is a option for the user to upload his image.I have created a popup block for image uploading. But my concern is to check , when the user will uplaod the image the size of the image will not be more than 10 mb ,and if the user will not upload any image then error message will show for uploading image.Means i have to check validation for two condition
1:The user should upload image before submitting
2:The image size will not be more than 10mb.
i have followed number of javascript code for client side validation but i   am not getting the end result.and also tried for client side code , that is also not working , i am really dumbed , can any one give me idea how to do this , sample code is well accepted.

thanks
Tanayya
0
Comment
Question by:tanu80
2 Comments
 
LVL 3

Accepted Solution

by:
mickdoc earned 500 total points
ID: 22796970
I don't know the javascript code but I would check it on submission. I used this recently:

(I assume you are using <asp:FileUpload id="fu" runat="server" />



if (fu.HasFile)
{
    string filename = HttpUtility.HtmlEncode(fu.FileName.ToLower());
    string extension = System.IO.Path.GetExtension(filename);
    System.Drawing.Image uploadedImg = System.Drawing.Image.FromStream(fu.PostedFile.ImputStream);
 
    float fuWidth = uploadedImg.PhysicalDimension.Width;
    float fuHeight = uploadedImg.PhysicalDimension.Height;
 
    if (extension == ".gif")    //Assuming you want to check for a particular file extension
    {
        if (fu.PostedFile.ContentLength > 10000000)   //Should be your filesize in bytes
        {
            //Put some rejection code here coz file is too big
        }
        else if (fuWidth > 180 || fuHeight > 180) //The max height and width
        {
            //Put some more rejection code here coz dimensions are wrong
        }
        else
        {
            //All is well, do what you need to do with the form... probably save the image like this
            fu.SaveAs("Your path and filename here");
        }      
    }
}
 
You can encase the whole lot in Try... Catch blocks in case the path to the file is bogus or something.
 
That should work for you.

Open in new window

0
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 22799139
You cannot do this in javascript due to security reasons. If javascript would be allowed to access your file system files I doubt you would be happy about this.

In addition to micdoc's post I'd like you to know that there is a default limit of web request in ASP.NET. It defaults to ~4MB. That means that users of your page will not be able to upload files larger than 4MB though you validate against 10MB.

Read this to learn how to handle situations like that
http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx

R
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question