How Multiple Image Upload ASP.NET C#

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

create a new ASP.NET Web Application.

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.     <head runat=“server”>
  6.         <title></title>
  7.     </head>
  8.     <body>
  9.         <form id=“form1” runat=“server”>
  10.             <div>
  11.                 <asp:ScriptManager ID=“SM” runat=“server”> </asp:ScriptManager>
  12.                 <asp:UpdatePanel ID=“UpdFileUpload” runat=“server”>
  13.                     <ContentTemplate>
  14.                         <asp:FileUpload ID=“FuImage” multiple=“multiple” runat=“server” />
  15.                         <asp:Button ID=“btnSaveFile” Text=“Save” runat=“server” onclick=“btnSaveFile_Click” /> </ContentTemplate>
  16.                     <Triggers>
  17.                         <asp:PostBackTrigger ControlID=“btnSaveFile” />– </Triggers>
  18.                 </asp:UpdatePanel>
  19.             </div>
  20.         </form>
  21.     </body>
  22.     </html>

Add the only useful and relevant namespace.

  1. using System;
  2. using System.Web;
  3. using System.IO;]

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. }

Leave a Reply

Your email address will not be published. Required fields are marked *