Introduction
When Microsoft introduced ASP.NET version 1.0, they added the ability for web applications to upload files from client machines onto the web server. This can be done using the HTML input file server control. This control allows the user to browse for the file to upload.
Now we will add three HTML input file server controls and allow the user to select three files. Upon pressing the Batch Upload button the selected files will be uploaded to the server. From there we will be filtering the uploaded files into two categories:
- Images
- Others
We will create two folders inside the root application folder, the first is called "Images" which will be used to save only the images (jpg, gif) being selected by the user and the second is called "Others" which will be used to save all other types of files.
HTML Code
Let's first see how the HTML code of the webform will look like with the three html input file server controls and the three corresponding labels:
The screenshot below demonstrates how the webform will look after we have added the specified controls.

Code Behind
To upload the files to the server we will use the following four system classes.
- HttpFileCollection
- HttpPostedFile
- Request.Files
- System.IO.Path
The HttpFileCollection class contains a collection of HttpPostedFile. To get the files selected by the user, we will use the Request.Files, which will return a collection of files, and store them inside the HttpFileCollection. The System.IO.Path is used to check the file extension and the filename. We will use the file extension to filter between images and other files types.
As you may have noticed we are looping through each HttpPostedFile in HttpFileCollection, saving the file extension in a variable and the file name in a second variable. Then we are checking for the extension. If it's an image we are using the HttpPostFile.SaveAs() method to save the image inside the "Images" directory and if it is another type of file we are saving it into the "Others" directory.
Next we’ll create a method called ShowMessages which’ll display the status of each file upload:
ShowMessage takes two parameters, the first is a string parameter and the second is an integer related to the index of HttpFileCollection being processed.
This method will be used to display a message beside each HTML input server control to tell the user if the upload succeeded or to display the error message if an error occurred. The below screenshot shows how the messages will be displayed after a successful upload process.

Complete code:
There are two things to take into consideration for this upload process to succeed:
- You have to modify the
maxRequestLength attribute of <httpRuntime> inside the Web.config file in order to be able to upload more than 4 MB size. Remember its value is in KB.
- Give access to the
ASPNET user account to the two folders we have created (Images, Others).
Conclusion
In this article, you saw how powerful the HTML file server control can be. Adding this capability into your web application will give the user the ability to upload multiple files to your web server.
About Haissam Abdul Malak
 |
Haissam Abdul Malak works as senior software developer in CCC. He has been developing web application using ASP.NET technology over the last 3 years. He achieved the Microsoft Certified Application Developer [MCAD] and been certified since 2006.
He is a regular contributor on the ASP.NET official f...
This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Ajax Control Toolkit: new controls, bug fixes
read more
NEW VERSION Ajax Control Toolkit
read more
Subtext Security Issue and Patch
read more
Using the RadFileExplorer for ASP.NET AJAX in a MOSS web application
read more
CodeDigest.Com Article,Codes,FAQs - April,2009
read more
Easy way to create a web-based AJAX SFTP Client application
read more
File Upload Control Using Silverlight
read more
How to use Ninject with ASP.NET MVC
read more
DomainDataSource Server Control: LINQ + Code Generation
read more
Handling Formats Based On Url Extension
read more
|
|
Please login to rate or to leave a comment.