Solved

File Upload Size Limit Sends "Cannot Find Server" error

Posted on 2006-07-19
11
880 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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