?
Solved

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

Posted on 2008-10-23
2
Medium Priority
?
1,811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
mickdoc earned 1500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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