Uploading Multiple Files Using ASP.Net 4.5

Background
 
In earlier versions of Visual Studio it was very difficult to do multiple file uploads because we had to write many lines of code but in ASP.Net 4.5 Visual Studio provides the feature to enable the upload of multiple files at a time without writing much code. So let us learn about the ASP.Net 4.5 File Upload control step-by-step.
 
File Upload Control

The File Upload control displays a text box control and a browse button that enables users to select a file to upload to the server.

The following are some of the common properties of the ASP.Net File Upload control:
  • AllowMultiple: Gets or sets a true or false value that specifies whether multiple files can be selected for upload, if true then it allows multiple file uploads, the default is false.
  • FileBytes: Gets an array of the bytes in a file that is specified using a System.Web.UI.WebControls.FileUpload control
  • FileContent: Gets a System.IO.Stream object that points to a file to upload using the System.Web.UI.WebControls.FileUpload control.
  • FileName: Gets the name of a file on a client to upload using the System.Web.UI.WebControls.FileUpload control.
  • HasFile: Gets a value indicating whether the System.Web.UI.WebControls.FileUpload control contains a file.
  • HasFiles: Gets a value indicating whether the System.Web.UI.WebControls.FileUpload control contains a files.
  • PostedFile: Gets the underlying System.Web.HttpPostedFile object for a file that is uploaded using the System.Web.UI.WebControls.FileUpload control.
  • PostedFiles: Gets the collection of uploaded files. 

The following are the methods of the ASP.Net File Upload control:

  • AddAttributesToRender: Adds the HTML attributes and styles of a System.Web.UI.WebControls.FileUpload control to render to the specified System.Web.UI.HtmlTextWriter object.
  • OnPreRender: Raises the System.Web.UI.Control.PreRender event for the System.Web.UI.WebControls.FileUpload control.
  • Render: Sends the System.Web.UI.WebControls.FileUpload control content to the specified System.Web.UI.HtmlTextWriter object, that writes the content to render on the client.
  • SaveAs: Saves the contents of an uploaded file to a specified path on the Web server.

Now let us demonstrate the preceding explanation by creating a sample web application as follows:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "UsingMultiUpload" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" and Add Web Form.
  5. Drag and drop one Button, a Label and a FileUploader control onto the <form> section of the Default.aspx page.

Now the default.aspx page source code will look as follows.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Article by Vithal Wadje</title>  
  8. </head>  
  9. <body bgcolor="gray">  
  10.     <form id="form1" runat="server">  
  11.         <br />  
  12.   
  13.   
  14.         <br />  
  15.         <div style="color: white">  
  16.             <h4>Article for C# corner</h4>  
  17.             <table>  
  18.   
  19.                 <tr>  
  20.   
  21.                     <td>Select Files</td>  
  22.                     <td>  
  23.                         <asp:FileUpload ID="FileUpload1" runat="server" />  
  24.                     </td>  
  25.                     <td></td>  
  26.                     <td>  
  27.                         <asp:Button ID="Button1" runat="server" Text="Uplaod" OnClick="Button1_Click" />  
  28.                     </td>  
  29.   
  30.                 </tr>  
  31.   
  32.             </table>  
  33.   
  34.   
  35.   
  36.         </div>  
  37.         <asp:Label ID="Label1" runat="server" Visible="false" ForeColor="LawnGreen" Text="Label"></asp:Label>  
  38.     </form>  
  39. </body>  
  40. </html> 
Now set the File Upload control AllowMultiple to true as in the following:
  1. <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" /> 
Create the folder in Solution Explorer by right-clicking to save uploaded Multiple files as in the following:
 
 
 
Write the following code for the Upload button click event to Upload and save files on the server folder as in the following:
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.     {  
  3.         foreach (HttpPostedFile htfiles in FileUpload1.PostedFiles)  
  4.         {  
  5.             getFileName = Path.GetFileName(htfiles.FileName);  
  6.             htfiles.SaveAs(Server.MapPath("~/UploadedFiles/"+getFileName));  
  7.           
  8.         }  
  9.         Label1.Visible = true;  
  10.         Label1.Text = FileUpload1.PostedFiles.Count.ToString() + " Files Uploaded Successfully";  
  11.     } 
 The entire code of the default.aspx.cs page will look as follows:
  1. using System;  
  2. using System.Web;  
  3. using System.IO;  
  4.   
  5. public partial class _Default : System.Web.UI.Page  
  6. {  
  7.     string getFileName;  
  8.     protected void Page_Load(object sender, EventArgs e)  
  9.     {  
  10.   
  11.     }  
  12.     protected void Button1_Click(object sender, EventArgs e)  
  13.     {  
  14.         foreach (HttpPostedFile htfiles in FileUpload1.PostedFiles)  
  15.         {  
  16.             getFileName = Path.GetFileName(htfiles.FileName);  
  17.             htfiles.SaveAs(Server.MapPath("~/UploadedFiles/"+getFileName));  
  18.           
  19.         }  
  20.         Label1.Visible = true;  
  21.         Label1.Text = FileUpload1.PostedFiles.Count.ToString() + " Files Uploaded Successfully";  
  22.     }  

Now run the application. The UI will look such as follows:
 
 
Now click on the Browse Button and select multiple files by Pressing the Ctrl button of the keybord as in the following:
 
 
Now click on Open after selecting the files then the File Upload control saves the comma (,) separated file paths as in the following:
 
 
Now click on the Upload button. The following message is shown as I have set to a label control as in the following:
 
 
 
Now refresh the folder we created to save the files to, it will look such as follows:
 
Now you have seen how to upload multiple  files with a minimal amount of code and effort.
Note
  • Do a proper validation such as if it has a file or not of the File Upload control when implementing.
  • For more details and explanation, download the Uploaded Zip file.
Summary

From all the preceding examples you have learned how to Upload multiple files. I hope this article is useful for all readers, if you have a suggestion then please contact me.


Similar Articles