Multiple Image Upload In ASP.NET C#

There are some simple steps to understand how to upload the multiple image file by the single file upload control.

Step 1. Now, create a new ASP.NET Web Application.

Step 2. Write the code, given below, in default.aspx page.

  1. <%@ Page Language="C#" AutoEventWireup="true"CodeBehind="UploadMultipleFileDemo.aspx.cs"  
  2. Inherits="MultipleFile.UploadMultipleFileDemo" %>  
  3.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.     <html xmlns="http://www.w3.org/1999/xhtml">  
  5.   
  6.     <head runat="server">  
  7.         <title></title>  
  8.     </head>  
  9.   
  10.     <body>  
  11.         <form id="form1" runat="server">  
  12.             <div>  
  13.                 <asp:ScriptManager ID="SM" runat="server"> </asp:ScriptManager>  
  14.                 <asp:UpdatePanel ID="UpdFileUpload" runat="server">  
  15.                     <ContentTemplate>  
  16.                         <asp:FileUpload ID="FuImage" multiple="multiple" runat="server" />  
  17.                         <asp:Button ID="btnSaveFile" Text="Save" runat="server" onclick="btnSaveFile_Click" /> </ContentTemplate>  
  18.                     <Triggers>  
  19.                         <asp:PostBackTrigger ControlID="btnSaveFile" /></Triggers>  
  20.                 </asp:UpdatePanel>  
  21.             </div>  
  22.         </form>  
  23.     </body>  
  24.   
  25.     </html>  
Step 3. Now Add the only useful and relevant namespace.
  1. using System;  
  2. using System.Web;  
  3. using System.IO;  
Step 4. Now, add the line of the code, given below, in default.aspx.cs.
  1. namespace MultipleFile  
  2. {  
  3.     public partial class UploadMultipleFileDemo: System.Web.UI.Page   
  4.     {  
  5.         protected void Page_Load(object sender, EventArgs e) {}  
  6.         protected void btnSaveFile_Click(object sender, EventArgs e) {  
  7.             HttpFileCollection _HttpFileCollection = Request.Files;  
  8.             for (int i = 0; i < _HttpFileCollection.Count; i++) {  
  9.                 HttpPostedFile _HttpPostedFile = _HttpFileCollection[i];  
  10.                 if (_HttpPostedFile.ContentLength > 0) _HttpPostedFile.SaveAs(Server.MapPath("~/a4d/ComposeEmail/" + Path.GetFileName(_HttpPostedFile.FileName)));  
  11.             }  
  12.         }  
  13.     }  
  14. }  
Step 5. Now, run your Application and you will get the output.

Step 6. Now, click browse and select one or more than one image.

browse

Step 7. Now, click on save button after select image files.

Step 8. Now, you can see in our client folder, where all the selected images are saved.

client folder

 

Next Recommended Reading File Upload Application In ASP.NET C#