Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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, …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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