Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 6885 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > FileUpload Server Control

FileUpload Server Control

1 vote(s)
Rating: 5 out of 5
Article posted by Ganeshji on 7/30/2010 | Views: 2727 | Category: ASP.NET | Level: Beginner red flag


Using this control, in ASP.NET one can upload files very easily. Though, it has few restrictions, yet I would say it is a fantastic Control.


Introduction

By using the FileUpload control, one can upload pictures, text files, or other file. After selecting the file, the FileUpload control doesn’t automatically send a file to the server. To allow the user to submit the form, you must explicitly provide a control or mechanism. An event is needed to be raised, and within the even handler method, the code to save a file or to handle the contents of the file are handled.

 

Properties

Description

FileBytes

Gives the uploaded file contents as a byte array.

 

FileContent

Gives the uploaded file contents as a stream.

 

FileName

Gives get the name of the uploaded file.

 

HasFile

It returns true, when a file is uploaded.

 

PostedFile

Gives the uploaded file wrapped in the HttpPostedFile object.

Table-1: Displaying the properties of FileUploadControl

Example showing uploading of file :



In this example, I have used Path Class to use the required Methods such as

  1. GetFileName,
  2. GetExtension etc.
Now, this Path class is available in System.IO namespace.
Here, i have restricted the FileUpload Control to upload only .jpg/.png/.bmp files. The uploaded file is saved using the SaveAs() method. Below method will save the file in the applications root/upload folder.


Default.aspx.cs





If user selects any file other than .jpg/.png/.bmp extension, the following output appears:



If a user forgets to Browse and clicks on the Upload Button, the following output appears:




If a user selects any file successfully, then the following output appears:




FileUploadControl using RegularExpressionValidator control


In this following snap, i have used RegularExpressionValidator Control to check for jpg/gif/png/bmp extension. If user selects some files of some other extension, error appears.

Default2.aspx




Output showing validation performed by the RegularExpressionValidator Control


This output appears when i choose a file of different extension.



Uploading 3 files at a time with a single Button Click





Here, you can select 3 files at a time and upload them in ‘C:\Upload’ location with  single Button Click event. To have a control on the uploaded files, I had to use HttpFileCollection class with the Request.Files property. In my example, I have worked with the FileName and the Location i.e., where it is saved.


Both these HttpFileCollection and HttpPostedFile are available in System.Web namespace.

HttpFileCollection lets us access the files and it also organizes the files which are being uploaded from a client as a file collection.

Request.File property gets the collection of files uploaded by the client in multipart MIME format. 

HttpPostedFile  provides properties and methods to access information about an individual file and also to read and save the file.



Default3.aspx.cs




By default, ASP.NET rejects a request that is larger than 4 MB. We can change this maximum by modifying the MaxRequestLength setting in the web.config file. This actually will set the largest allowed file in kilobytes.

          If you change MaxRequestLength property, you must be concerned about the executionTimeout property also. This property sets the time for a request to attempt to execute to the server before ASP.NET shuts down the request. The default setting is only 90 seconds. Hence, if you increase the size of the MaxRequestLength property, you should also increase the executionTimeout property.

The RequestLengthDiskThreshold setting in the web.config file determines how a form post is buffered to the file system. Therefore, by accessing the RequestLengthDiskThreshold property, you can determine the amount of data that is buffered in server memory for a request. When the size of the file passes the RequestLengthDiskThreshold setting, the remainder of the file is buffered to the file system.

          Any post larger than 80 KB is stored in the file buffer. If you want, you can modify the RequestLengthDiskThreshold property.


The following web.config file enables files up to 10MB to be posted. The buffering threshold is changed to 100KB. 





Conclusion


I have tried to explain this control, so that my readers can enjoy reading this. Please forward any suggestions or comments after reading this article.

Reference

http://www.c-sharpcorner.com/uploadfile/mahesh/fileupload10092005172118pm/fileupload .aspx


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:1 year(s)
Home page:
Member since:Saturday, July 24, 2010
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

How the ViewState is maintained by ASP.NET?

In case we have to give ability to the user to select the columns that should appear in the GridView, we can follow this approach.

Both .Net Web services and .Net Remoting are Microsoft solutions for distributed application. Before choosing one of them for application development, let’s identify the differences between these technologies.

In this article, we shall learn how to select GridView rows and persist the selection during GridView pagination.

This is Basically use to change the address bar icon of your website.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:21:20 AM