Monday, January 31, 2011

File Upload Control in Asp.Net

Introduction

ASP.Net 2.0 comes up with a completely new File Upload Control. This is how you can declare the file upload control.
< asp:FileUpload id="FileUpload1? runat="server" />

Some useful properties
FileUpload1.FileName :- Will give File Name with the full Path.
FileUpload1.PostedFile.FileName :- Will give only the file name.
FileUpload1.PostedFile.ContentLength :- will give size of the file in Bytes.
FileUpload1.PostedFile.ContentType :- will give MIME type of uploaded file, i.e. "image/gif".

Web.Config Settings
By Default, maximum allowed size for file is 4MB. To allow files larger than the default of 4MB, one need to change the web.config.There is a parameter called maxRequestLength which takes value in the KB.
< system.web>
< httpRuntime executionTimeout="1000? maxRequestLength="1048576?/>
< /system.web>


The maxRequestLength property dictates the size of the request made to the Web server.When you upload files, the file is included in the request.

This example changes the maxRequestLength property's value to 1048576KB (around 1GB). With this setting in place, end users can upload 1Gb file to the server.

There is a one more property named "executionTimeout".This property sets the time (in seconds) for a request to attempt to execute to the server before ASP.NET shuts down the request (whether or not it is finished). The default setting is 90 seconds.
The end user receives a timeout error notification in the browser if the time limit is exceeded. If you are going to permit larger requests, remember that they take longer to execute than smaller ones. If you increase the size of the maxRequestLength property, you should examine whether to increase the executionTimeout property as well.


Role of IIS
Uploading files in ASP.NET is very inefficient.When you pick a file and submit your form, IIS needs to suck it all in and only then you have access to the properties of uploaded file(s).You can not do about the fact that you have to sit through a long upload and wait. Neither can you display a meaningful progress bar because there's no way to know how much is transmitted at any given time. Once IIS buffers your upload, ASP.NET takes it from there.

You might receive errors when your end users upload files to your Web server through the FileUpload control in your application. These might occur because the destination folder on the server is not writable for the account used by ASP.NET. If ASP.NET is not enabled to write to the folder you want, you can enable it using the folder's properties.

No comments:

Post a Comment

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...