Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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