Saturday, November 24, 2007

File Upload in ASP.NET 2.0

Where I migrated to ASP.NET 2.0 there was a whole new world waiting for me, lots of new features, new control and many more... In all that there was one more and nice feature is "FileUpload" which made life of programmer more easy.

This control is not similar as that of ASP.NET 1.x, here you had to take a few extra steps to make use of it such as
  1. In Classic ASP or ASP.NET 1.x we have to add enctype="multipart/form-data" in form tag.
  2. In classic many programmers use to write their own components to read the stream and save the data as file(s) or they use to use third party controls.

While using this one has to know few key things about the control to get best out of it.

  1. The default value for the maxRequestLength parameter in the section of the Machine.config (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) file is 4096 (4 megabytes). As a result, files that are larger than this value are not uploaded by default. We can overwite this by required value in the web.config. This will change the value to (8 megabytes)
  2. The value given the executionTimeout attribute is the number of seconds the upload is allowed to occur before being shut down by ASP.NET.
  3. During the upload process, ASP.NET 1.x loads the whole file in memory before the user can save the file to the disk. Therefore, the process may recycle because of the memoryLimit attribute of the processModel tag in the Machine.config file. The memoryLimit attribute specifies the percentage of physical memory that the ASP.NET worker process can exhaust before the process is automatically recycled. Recycling prevents memory leaks from causing ASP.NET to crash or to stop responding. But in ASP.NET 2.0 there is a new attribute "requestLengthDiskThreshold" in the httpRunTime element in the web.config that specifies a disk buffer. Changing this to 8192 and you are done.
  4. Make sure that ASP.NET account is enabled to write to the folder you want, simply open up Microsoft Windows Explorer and navigate to the folder to which you want to add this permission. Right-click on the folder and then select Properties. In the Properties dialog box, click on the Security tab and make sure the ASP.NET Machine Account is included in the list and has the proper permissions to write to disk.

Important Note:
This attribute is available in 2.0 and above,
The RequestLengthDiskThreshold property specifies the input-stream buffering threshold limit in number of bytes. Its value should not exceed the MaxRequestLength property value. After a request entity exceeds this threshold, it is buffered transparently onto disk.
With this, fileupload has improved a lot because the requestLengthDiskThreshold value sets the amount of the request that is cached in memory, and data beyond this value is temporarily written to disk. For more info on this please visit
http://msdn2.microsoft.com/en-us/library/h2e8928c.aspx

Here is one more nice feature (progress bar) added to it
http://www.brettle.com/neatupload

Have a look at the important node in web.config for the FileUpload control
http://msdn2.microsoft.com/en-us/library/e1f13641.aspx

For More details
http://msdn2.microsoft.com/en-us/library/aa479405.aspx
http://aspnetresources.com/articles/discuss/dark_side_of_file_uploads.aspx


In case if you any information here is wrong please feel free to write to me Bharat.Mane@gmail.com

Thank You
Bharat Mane