Asp.Net 2.0 File Upload Control

Virendradugar
Posted by in ASP.NET category on for Beginner level | Views : 7453 red flag

This article explains the file upload control.
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.


Enjoy...



 
Page copy protected against web site content infringement by Copyscape

About the Author

Virendradugar
Full Name: Virendra Dugar
Member Level: Silver
Member Status: Member,MVP
Member Since: 8/11/2009 4:14:05 AM
Country: India

http://jquerybyexample.blogspot.com
Virendra Dugar is experienced Senior Software Developer with over 5 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc. Visit his blogs : http://jquerybyexample.blogspot.com

Login to vote for this post.

Comments or Responses

Posted by: Raja on: 9/4/2009
Hi Virendraduga,

Thanks for this detailed article on FileUpload control. It would have been nicer if you would also shown the code to save the file on the server. (I mean C# code).

Thank, I am waiting for that....
Posted by: Virendradugar on: 9/7/2009
Hi Raja,

Thanks.

As requested here is the one line code which will save
selected file on the server.

FileUpload1.SaveAs(@"D:/" + FileUpload1.FileName);


"D:/" can be replaced with your own path.

Hope this helps...

Login to post response

Comment using Facebook(Author doesn't get notification)