Monday, 12 September 2011

(Nearly) All tricks to get large file uploaded in ASP.NET

The file upload control in ASP.NET can come in handy when you want to upload files unto your server. It's long been said a file as large as 2 GB can be handled by tweaking a few parameters. However, it turns out that a lot more hurdles are there awaiting to be solved and it is not until today that I finally get it working. Therefore, I though listing all the necessary changes and traps I encountered here together with the solution may benifit a poor fellow like me in the future.

First some brief introduction about my server enviroments and some considerations:

I run a few web applications within our main site and some of the applications would need file upload functionality. We may have new server in the near future to be dedicated to the applications. So instead of making systematic change in machine.config which may not be copied to new machine, all the changes are made in web.config in each application when applicable.   

The changes to be made:

1. HTTP max request length limit and request time out limit.

File to modify: web.config of your web application (or machine.config if you preffered).

Content to add/modify:

<system.web>
  <httpRuntime maxRequestLength="XXXX" executionTimeout="XXXX"/>
 </system.web>

Notice: Request length in KB and timeout val in MINI SECOND.

Comments: These are all the changes need as Google suggested but is just the starting point for me.


2. If URLScan is used, change its request length limit.

File to modify: urlscan.ini, it's in the same folder as URLScan installed, normally C:\Windows\system32\inetsrv\urlscan

Content to add/modify: MaxAllowedContentLength = XXXX

Notice:Content length in BYTE.

Comments: It took me a LONG time to discover this.


3. If session is implemented, change session time out.

File to modify: web.config of your web application (or machine.config if you preffered).

Content to add/modify:

 <system.web>
      <sessionState timeout="XXXX"  />
 </system.web>

Notice:Timeout value in MINUTES.

Comments: I store uploaded file name into session variable which would be cleared when session timed out. The upload page also reset it self as the session timed out.

4. If implimented form authentication, change form authentication timeout.

File to modify: web.config of your web application (or machine.config if you preffered).

Content to add/modify:

 <system.web>
      <authentication mode="Forms">
             <forms timeout="XXXX"/>
      </authentication>
 </system.web>

Notice:Timeout val in MINUTES.

Comments: User would be logged out and redirected to the log in page when file uploading takes longer than this limit.


5. If file uploaded is to be stored into database, change the db connection timeout in data source configuration.

File to modify: The aspx files that handles the file upload.