Multiple Image Upload Using File Upload In ASP.NET C#

Introduction

 
This article helps you to know and implement Single or multiple image upload in asp.net c# using Fileupload control.
 
Below are some simple steps to upload multiple images in a single upload using file upload in asp.net c#,
 
Step 1
 
Create a new Asp.net Web Application using visual studio IDE. 
 
Step 2
 
Write below code in designer page(.aspx). 
 
You need to add reference of Ajaxtoolkit in order to use updatepanel control as highlighted below in yellow,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiplePhotoupload.aspx.cs" Inherits="MultiplePhotoupload" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <h5 class="text-capitalize px-3">Image Upload</h5>  
  12.                 <asp:ScriptManager ID="SCPTMGR" runat="server"></asp:ScriptManager>  
  13.                 <asp:UpdatePanel ID="UpdimageUpload" runat="server">  
  14.                     <ContentTemplate>  
  15.                         <asp:FileUpload ID="FileuploadImage" multiple="multiple" runat="server" />  
  16.                         <asp:Button ID="btnSave" Text="Save Image" runat="server" OnClick="btnSave_Click"/>  
  17.                     </ContentTemplate>  
  18.                     <Triggers>  
  19.                         <asp:PostBackTrigger ControlID="btnSave"/>  
  20.                     </Triggers>  
  21.                 </asp:UpdatePanel>  
  22.             </div>  
  23.         </form>  
  24.     </body>  
  25. </html>   
Control and Reference Explaination
 
Here I am using ajax control like Scriptmanger, Updatepanel in order to provide Async communication between client and server. Update Panel basically used when you want only a specific part of the page to get refresh instead of the complete page so you just need to put control code inside the update panel control.
 
In order to use ajax control, we need to add a reference of Ajaxtoolkit in your visual studio IDE as below. Once add a reference to the ajax tool kit the DLL will get added to the bin folder as below. 
 
Multiple Image Upload Using File Upload In ASP.NET C#
 
Multiple Image Upload Using File Upload In ASP.NET C#
 
Step 3
 
Add below namespce and piece of code in .aspx.cs file,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web;  
  8. using System.IO;  
  9. public partial class MultiplePhotoupload: System.Web.UI.Page {  
  10.     protected void Page_Load(object sender, EventArgs e) {}  
  11.     protected void btnSave_Click(object sender, EventArgs e) {  
  12.         HttpFileCollection _HttpFileCollection = Request.Files;  
  13.         for (int i = 0; i < _HttpFileCollection.Count; i++) {  
  14.             HttpPostedFile _HttpPostedFile = _HttpFileCollection[i];  
  15.             if (_HttpPostedFile.ContentLength > 0) _HttpPostedFile.SaveAs(Server.MapPath("~/image/banner/" + Path.GetFileName(_HttpPostedFile.FileName)));  
  16.         }  
  17.     }  
  18. }   
Multiple Image Upload Using File Upload In ASP.NET C#
Step 4
 
Build the application and run as below,
 
Multiple Image Upload Using File Upload In ASP.NET C#
Step 5
 
Click on the browse button and browse one or multiple images (using ctrl) as below,
 
Multiple Image Upload Using File Upload In ASP.NET C#
Multiple Image Upload Using File Upload In ASP.NET C#
Step 6
 
Click on the Save image button then all images gets uploaded to the specified location as below,
 
Multiple Image Upload Using File Upload In ASP.NET C#
 

Summary

 
With this article, I explained how to upload single or multiple image upload using file upload control in asp.net c#.


Similar Articles