473,387 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I try trap fileUpload(Ajax Toolkit) exception?

I am using FileUpload(Ajax Toolkit) within my application.
I have placed a try-catch around the function call to initiate the file upload. When I try to upload any file greater than 4 megabytes, the application crashes and does not propagate the exception to my application.

How can I trap this exception?

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void UploadFile_Click(object sender, EventArgs e)
  3.     {
  4.  
  5.         //Initialize
  6.         lError.Text = "";
  7.         lInfo.Text = "";
  8.  
  9.         bool IsActive = true;
  10.         string ExtType;
  11.         string UserName = (string)Session["UserName"];
  12.  
  13.         try
  14.         {
  15.  
  16.             if (FileUploadPDF.HasFile == false)
  17.             {
  18.                 // No file uploaded!
  19.                 lError.Text = "You have not selected a file to upload.";
  20.  
  21.             }
  22.             else
  23.             {
  24.                 int FileSize = FileUploadPDF.PostedFile.ContentLength;
  25.                 string FileName = FileUploadPDF.FileName;
  26.                 ExtType = FileName.Substring(FileName.Length - 3, 3).ToLower();
  27.  
  28.                 if (FileSize > 4069000)
  29.                 {
  30.                     lError.Text = "Your file was not uploaded because it exceeds the 4 MB size limit.";
  31.                     return;
  32.                 }
  33.                 if ((ExtType != "pdf") & (ExtType != "txt"))
  34.                 {
  35.                     lError.Text = "This application only accepts pdf and txt files";
  36.                     return;
  37.                 }
  38.                 if (VehicleMaintenanceID == 0)
  39.                 {
  40.                     lError.Text = "A maintenance record must exist before you can attach a document";
  41.                     return;
  42.                 }
  43.  
  44.                 MaintSummary ms = new MaintSummary();
  45.  
  46.                 int result = ms.UploadDocuments(FileName,
  47.                                                 IsActive,
  48.                                                 FileUploadPDF.FileBytes,
  49.                                                 ExtType,
  50.                                                 VehicleMaintenanceID,
  51.                                                 UserName);
  52.  
  53.                 if (result == 0)
  54.                 {
  55.                     // Display the uploaded file's details
  56.                     lInfo.Text = string.Format(
  57.                             @"Successfully Uploaded file: {0}<br />
  58.                             File size (in bytes): {1:N0}<br />
  59.                             Content-type: {2}",
  60.                             FileUploadPDF.FileName,
  61.                             FileUploadPDF.FileBytes.Length,
  62.                             FileUploadPDF.PostedFile.ContentType);
  63.  
  64.                 }
  65.                 else
  66.                 {
  67.                     lError.Text = "Uploading document failed";
  68.                 }
  69.  
  70.             }
  71.  
  72.         } // end of try
  73.         catch (System.Exception ex)
  74.         {
  75.             lError.Text = ex.Message;
  76.         }
  77.  
  78.     }
  79.  
Sep 14 '10 #1
3 2583
mzmishra
390 Expert 256MB
It's a config issue. The reason you can't hook in is because IIS doesn't even send the request to your app when the file size is too large. Try one of the following:

You can modify your machine or web config maxRequestLength value for the application
Sep 15 '10 #2
You will get an unhandled httpexception that you can catch from an httpmodule or from your global.asax. I worked on a weird app where we caught the error in the global.asax, displayed an alert, and then redirected them back to the page they came from.
Sep 22 '10 #3
lcddr
1
When a file exceeds upload limit, the application isn't notified. You must handle the response on client. See:
http://it.expertmonster.com/question...eptio-114.html
Dec 21 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
2
by: Cc | last post by:
just dumb question, is system.exception is a global exception? for example rather than use odbcexception to catch odbc error I can use system.exception
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great...
12
by: Phil Z. | last post by:
After migrating an ASP.NET 1.1 application to 2.0 we were getting "Cannot access a closed file" errors when uploading. I found a number of post on the subject and have since moved from using an...
1
by: Harinath | last post by:
Hi All, I am working on a portal using .net2.0. In one of my page, i have FileUpload control. I am doing some server side validation for form fields in general. if there is any validation...
0
by: thupham | last post by:
I use Visual Studio 2003, and Ajax.dll In design, I have : 1 FileField, named: FileInput, set runnat="server" 1 Button (of Html toolbox), named: cmdUpload 1 Label (of HTML...
9
by: vital | last post by:
Hi, I have a User control which has a fileupload control. With the requirements I placed the user control in a UpdatePanel of ASP.NET Ajax and the fileupload.postedfile.filename giving error. ...
6
by: GaryDean | last post by:
Is there any way to make an asp FileUpload control work within an UpdatePanel? Thanks, Garyh
4
by: pechar | last post by:
Hi All, This question has been asked many times and I've checked most of the sites with resolutions. None worked for me. 1) I have a page which is a content page to a Masterpage . 2) I have a...
11
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have run into a situation that if a page/tab that uses the Ajax toolkit (using .net version 3.5) is closed before the Ajax enable controls complete loading, then IE locks up. Does it in both IE7...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.