Solved

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

Posted on 2008-10-23
2
1,797 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now