p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Validating the size of a file being uploaded by the FileUpload control (http://p2p.wrox.com/showthread.php?t=87232)

malie22001 April 11th, 2012 10:30 PM

Validating the size of a file being uploaded by the FileUpload control
Hi guys,

I have a little problem I'm struggling with here. I have a DetailsView control, which I have converted into templates and replaced some of its fields with regular server controls. One of such controls is the FileUpload control.

I want the user to be able to upload an image that will serve as a product avatar. The problem is, I need to make sure that the image being uploaded is not bigger than 2MB.

I am tapping into the DetailsView control's ItemInserting Event to get the file info from the FileUpload control and do a test on the size of the file before assing the e.Values property a file URL.

The problem is that the run time seems to be ignoring my chek for file size and going straight through the rest of the code.

The file size limit of the FileUpload control is determined by the httpRunTime element in the web.config file. I have set this element to accept only a file of size 2MB. However, I want a nice way to let the user know that they are trying to upload a large file.

So far, I am not getting any luck, my code is just being ignored and IE is displaying an unfriendly error message.

Can anyone help me out?

Thanks guys; you are the best!

PS: In know this problem might not be directly related to the book; however, the book mentioned using the FileUpload control within the DetailsView control to upload a file. I am following the same technique, but just need a way to prevent the user from uploading a huge file. I try doing this with the FileUpload control outside of the DetailsView control's ItemInserting event and it works, but within, it doesn't work...

Imar April 12th, 2012 04:09 PM

Hi there,

You shouldn't limit the size in web.config. Instead you need to complete the upload and then check its size in your code....

A Google search for limit file upload size results in this useful post: http://forums.asp.net/t/1138117.aspx/1



malie22001 April 12th, 2012 04:55 PM

Exactly; that is what I am trying to do, but it sems all the code that I have written to test te size are being ignored. I have even set a break point in the code, but as long as the size of the file is above 4MB, my code is skipped and te browser displays an error. I don't get to my custom error message. Here is the code I have written within the ItemInserting event handler of the DetailsView control:


protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        FileUpload imageUpload = (FileUpload)DetailsView1.FindControl("FileUpload1");
        if (imageUpload.HasFile)
            int fileSize = imageUpload.PostedFile.ContentLength;
            if (fileSize < 2000000)
                string virtualFolder = "~/ServiceImages/";
                string physicalFolder = Server.MapPath(virtualFolder);
                string filename = Guid.NewGuid().ToString();
                string extension = Path.GetExtension(imageUpload.FileName);
                imageUpload.SaveAs(Path.Combine(physicalFolder, filename + extension));
                e.Values["ServiceID"] = Convert.ToInt32(DropDownList1.SelectedItem.Value);
                e.Values["DateAdded"] = DateTime.Now;
                e.Values["ImageUrl"] = virtualFolder + filename + extension;
                erroLabel.Text = "File size too huge.";
                erroLabel.Visible = true;
            erroLabel.Text = "You have not selected a file.";
        //catch (SqlException ex)
        //catch (Exception ex)

Please advice.

Thank you.

malie22001 April 12th, 2012 05:04 PM


My code works, but what I didn't do was to stop the insert into the database by using e.Cancel. I have added those simple lines and te world is safe again.

Gees!! Programming is hard.

Thanks for your help.

Imar April 12th, 2012 05:06 PM

Ah, that explains it.....


All times are GMT -4. The time now is 02:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.