Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 11th, 2012, 10:30 PM
Authorized User
Points: 194, Level: 3
Points: 194, Level: 3 Points: 194, Level: 3 Points: 194, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default 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...
  #2 (permalink)  
Old April 12th, 2012, 04:09 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old April 12th, 2012, 04:55 PM
Authorized User
Points: 194, Level: 3
Points: 194, Level: 3 Points: 194, Level: 3 Points: 194, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

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:

Code:
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) 
    { 
        //try 
        //{ 
        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; 
            } 
            else 
            { 
                erroLabel.Text = "File size too huge."; 
                erroLabel.Visible = true; 
            } 
        } 
        else 
        { 
            erroLabel.Text = "You have not selected a file."; 
        } 
 
         
 
         
        //} 
        //catch (SqlException ex) 
        //{ 
 
        //} 
        //catch (Exception ex) 
        //{ 
 
        //} 
 
 
    }
Please advice.

Thank you.

Last edited by malie22001; April 12th, 2012 at 04:56 PM.. Reason: error
  #4 (permalink)  
Old April 12th, 2012, 05:04 PM
Authorized User
Points: 194, Level: 3
Points: 194, Level: 3 Points: 194, Level: 3 Points: 194, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry.

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.

Last edited by malie22001; April 12th, 2012 at 05:14 PM..
  #5 (permalink)  
Old April 12th, 2012, 05:06 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ah, that explains it.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating the size of a file being uploaded by the FileUpload control malie22001 ASP.NET 4 General Discussion 0 April 8th, 2012 04:22 PM
How to set limit fot uploaded photos size in PIXEL FeGan PHP How-To 0 December 3rd, 2011 01:10 PM
how do i determine size of file being uploaded? kaos_frack Beginning PHP 3 January 10th, 2007 03:48 PM
FileUpload control -- file size detection ekwong ASP.NET 2.0 Basics 3 November 7th, 2006 06:32 PM
Verifying uploaded File Size in Client-side Ushhh ASP.NET 1.0 and 1.1 Professional 5 August 30th, 2006 07:13 AM





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