Solved

File Upload Size Limit Sends "Cannot Find Server" error

Posted on 2006-07-19
11
882 Views
Last Modified: 2007-12-19
I have a file upload control and everything works fine until I have a file larger than the limit (which is 4MB). I put the following code:

If FileUpload1.PostedFile.ContentLength < 4000000 Then
                FileUpload1.SaveAs(myFilePath)
                Label1.Text = "Received " & FileUpload1.FileName & " Content Type " & FileUpload1.PostedFile.ContentType & " Length " & FileUpload1.PostedFile.ContentLength
            Else
                Label1.Text = "The File is too large."
            End If

But it doesn't do anything. The code above works if I set it to a lower value and upload a file that is less than 4MB.

Does anyone know how I can cancel the upload if it is more than the limit (4MB), and display an error message in my Label1.text?
0
Comment
Question by:Skytide
[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
11 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 250 total points
ID: 17140488
The reason why your code is not working on files greater than 4MB is that it is IIS that is blocking the upload and throwing the error before it gets to your code.  It is a pain to capture this error message.  I have not worked with this for a while but we had the same problem.  YOu could try an exception handler in the global.asax  but I don't remember that working for me.  You might have to just create a custom error page and set it in iis so that when the error occurs it redirects the user back to your page with a query string saying what the problem was.   Good luck.
0
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 250 total points
ID: 17140874
jjardine is correct.

However, the real issue is that you _shouldn't_ display such a message in your label. Let IIS handle this. (The max size is configurable via config files by the way.) If you try to handle it yourself, then you will only be able to handle it in your code _after_ the large file has already been uploaded. This is bad, because presumably the reason you want to limiut the file size is to protect the server, and the server has already suffered the performance consequences of the large upload by the time you can handle it.

If you really want to customize the warning, look into IIS's support for custom error pages for the predefined HTTP status codes.
0
 
LVL 14

Expert Comment

by:jjardine
ID: 17141192
I think Justin is clarifying what I was trying to say at the end regarding the custom error pages.  I didn't know if there was a way to se tthe custom error page as an asp page that somehow redirects back to your page so the user doesn't see that page.  I agree with Justin that the limit is there to protect.  Although it is a pain in the neck sometimes.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:vinodhsomasekharan
ID: 17143725
Hai,

Normally asp.net will allow to upload files of size less than 4MB ( Default) we can override this in web.config of our application

we have to add the following in u r web.config
here "maxRequestLength" attribute refers to the size of file in KB by increasing this u can upload large size files

<system.web>
 <httpRuntime
            executionTimeout="90"
            maxRequestLength="30096"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
        />
   
 </system.web>

    I think one more constraint is there..
    What's u r RAM size..  ( ASp.net process by default can utilize 60% of u r RAM's memory size )
   In the processModel tag of machine.config the default 60% will be set.  
   the attribute is    memoryLimit="60"
    if the file u r uploading exceeds the 60% of RAM'S Memory the asp.net process will be RESET.
    So if its the condition u have to increase the % in the processModel Tag.
    just increase the value of attribute  "memoryLimit"  

    Also the best way will be to split u r file into equal sized chunks..( eg. 2 MB) each and upload it... it will surely will not  
   make excess use of u r cpu memory..

Try this code ( in c#)

         string filepath  = rec.receiptFilePath + rec.receiptFileName;

            // Identify the file name.
           string  filename  = rec.receiptFileName;

                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=" + filename.Trim() + "");
                    Response.ContentType = "application/octet-stream";
 
                int chunkSize  = 1048576;
                byte[] bytdocument = new byte[chunkSize];
                int bytesRead = 0;
                System.IO.FileStream inputstream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.Read);
                    int length = (int)inputstream.Length;
                bytesRead = inputstream.Read(bytdocument, 0, chunkSize);
                 while (bytesRead > 0)
                    {
                         // Write the data to the current output stream.
                         Response.OutputStream.Write(bytdocument, 0, length);
                         Response.Flush();
                         bytesRead = inputstream.Read(bytdocument, 0, chunkSize);
                    }
                    Response.End();


With regards
Vinu
0
 

Author Comment

by:Skytide
ID: 17150425
I'm not really worried about uploading anything more than 500Kb, so the 4Mb limit is more than enough for my file uploads. I am more concerned about having a custom error page that can be displayed when someone tries to upload something larger than 4Mb.

However, it may seem like it would be a better idea to just let IIS handle the error (from the comments above).
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17150535
>> it may seem like it would be a better idea to just let IIS handle the error

That would be my opinion. Otherwise, you are making your server vulnerable.
0
 
LVL 14

Expert Comment

by:jjardine
ID: 17150540
Yeah.. If it is something your users are aware of (the limit in size) then replacing the default error page for that error would be a good solution to make them aware of the situation.  Then they could press back and change the file.
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 17422941
My comments above answered the original question and also provided additional info about the pros/cons of doing so.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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