Solved

File Upload Size Limit Sends "Cannot Find Server" error

Posted on 2006-07-19
11
877 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 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

19 Experts available now in Live!

Get 1:1 Help Now