?
Solved

File Upload Size Limit Sends "Cannot Find Server" error

Posted on 2006-07-19
11
Medium Priority
?
889 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
8 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 1000 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 1000 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Watch the working video to know how to import Outlook PST/OST files to Amazon WorkMail. Kernel released this tool which is very easy to use and migrate single or multiple PST and OST files to Amazon WorkMail. To know more about Kernel Import PST to …

589 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