codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

Multiple File Upload in ASP.Net

Article by: Pratik Desai (8/29/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: In this article we will upload multiple files using the HtmlInputFileControl and Files property of the Request object that return a reference to HtmlFileCollection which holds collection of HttpPostedFile objects
Viewed: 53151 times Rating (103 votes): 
 3.7 out of 5
 Rate this Article  Read Comments  Post Comments

Multiple File Upload in ASP.Net



Introduction


File uploading always has been challenging. .Net framework comes with pre built class that has made file uploading relatively simple. In this article we will upload multiple files using the HtmlInputFileControl and Files property of the Request object that return a reference to HtmlFileCollection. But first lets get familiar with the basics of file uploading.

Basics of File Uploading


1.Uploading a file from the client to the web browser
The following are the essential ingredients for uploading the file from the client.
The form declaration should use encryption of multipart/form-data and method of post.
<form id=�fileUpload� method=�post� runat=�server� enctype=�multipart/form-data�>
An html server control for the user to select a file.
<input id=�selectFile� type=�file� runat=�server�>
This will provide the user with a text box and a browser button to select the required file.
And finally an upload button
<input type=�button� value=�Upload� id=�Button1� runat=�server� onServerClick=�Button1_Click� >
This upload button with force the client server roundtrip calling our Button1_Click method where the processing logic will reside.

Receiving the File
When we use single file control we can use the PostedFile property of the file control. This property is of type HttpPostedFile type. If the PostedFile property is null than the client submitted no file.

If Not (selectFile.PostedFile Is Nothing Or selectFile.PostedFile.FileName = "" Or selectFile.PostedFile.ContentLength < 1) Then

�File is present
else
�File is not present
End if

Saving the File
Use the FileName property and SaveAs method of PostedFile to Save the File.
m_strFileName = selectFile.PostedFile.FileName
selectFile.SaveAs(m_strFileName)
While saving the file to the disk it is important to remember that the aspnet account should have write permission to the folder where you are saving the file.
Moreover the web server by default has a limit of 4MB as uploading size of the file. If we you want to upload a larger size file you will have to change the machine.config files httpRuntime settings.

Uploading Multiple Files


I will outline the solution of uploading multiple file and then walk you through it step wise.



Select All Code


On the client side as described above, add the form encryption tag, an upload button whose onServerClick calls uploadMultipleFiles_Clicked and the required number of HtmlInputFileControl as the number of files needed to be uploaded for our example it is two.

On the server side, the UploadMultipleFiles_Clicked method is where the processing logic resides. First define the location where the files need to be uploaded and finally be saved on the server. The Request object has Files property that defines the instance of HttpFileCollection class, which contains HttpPostedFile objects. Loop through the collection and access each HttpPostedFile object. For each HttpPostedFile object check to see whether the client actually posted the file. If a PostedFile is present get the Full path of the file using the FileName property of HttpPostedFile and than using System.IO.Path just get the file name. Finally invoke the SaveAs method and save the individual files to the desired location.

Conclusion


The flexibility that .net framework has provided helps us to achieve uploading multiple files. Number of Files can be uploaded at the same time by changing the number of HtmlInputFileControl on the client side while your server side code does not change, as it just needs to loop through Request.Files Collection.





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Multiple File Upload in ASP.Net'
Posted by :  nmain at 10:47 on Wednesday, November 19, 2003
Sorry - I am new to .net. I used notepad to create a aspx file and copied the above code example in and uploaded it to a directory on my webserver with appropriate asp.net permissions. When I call the aspx file I get the following error :

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The Runat attribute must have the value Server.

Source Error:


Line 27: </HTML>
Line 28:
Line 29: <script runat=�server�>
Line 30: Sub UploadMultipleFiles_Clicked(ByVal Sender As Object, ByVal e As EventArgs)
Line 31:


Source File: D:\WWWRoot\nsmaincom\www\content\asp.net\multiplefileupload\multiplefileupload2.aspx Line: 29

Any ideas ...


Best Regards

Nathan Main
Posted by :  petrovserg at 14:00 on Wednesday, December 10, 2003
Hi!
Just correct the line 29.
<script runat="server">.
Should be:
<script language="vb" runat="server">
Good luck!
Posted by :  drliebs at 14:54 on Friday, July 02, 2004
Awesome. After correcting line 29 it worked great. Easy to add additional file fields!

Thanks alot!

Posted by :  thedarkest1 at 06:49 on Thursday, March 17, 2005
Hello,

I am looking for a script that allows my clients to upload images to my server directly. I have little programming knowledge and do not know much about .net programming. Can I just upload this script and use it or do I need specific server software and write permissions?

Thanks for any help you may be able to offer.


Andrew
Posted by :  swati goila at 00:55 on Tuesday, March 29, 2005
Hello

This helps a lot. but what if i want multiple language support on the browse button. how can i change the caption on the browse button.


Swati
Posted by :  SteveW at 06:15 on Thursday, April 14, 2005
Hi There,

Is there a way of getting the multifile names from a multi select file dialogue box?

Best Regards,

Steve Wilson.


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
MagicGrid
by Abhijeet Kaulgud
MagicGrid is an all-in-one grid for ASP programmers. It is a 3 Level Hierarchial Grid. You can Add, Edit, Delete Items under all the three levels. You can also cut-copy-paste Items from one level to other, It happens just by drag & drop!
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP.NET Forum Source Code
by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic.
The asp:listbox control
by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>.








Recent Forum Threads
• Re: sorting and Linked list
• Re: need help linked list
• Re: Help with arrays
• Re: Reading from a file
• Re: Why Use Method?
• Re: Help with a simple program
• Re: need help with quiz
• Re: Help with filesystem object & displaying in a table
• Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005