Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Tue, 30-Sep-2014 Authors
All Time Authors
Sourav.Kayal
39750
SheoNarayan
38050
Niladri.biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads
 Article

FileUpload Server Control

Ganeshji
Posted by under ASP.NET category on for Beginner level | Views : 6143 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)
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


Page copy protected against web site content infringement by Copyscape
About the Author

Ganeshji

Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji

Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)