Solved

File Upload Size Limit Sends "Cannot Find Server" error

Posted on 2006-07-19
11
870 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now