Multiple Image Upload In ASP.NET Using C#

Multiple image uploads is possible in ASP.NET, using C#. There are some simple steps to understand. How to upload multiple image file by single file upload control?
 
Step 1

Now, create a new ASP.NET Web Application.

Step 2

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