Multiple File Upload in ASP.NET With Example

The FileUpload control enables you to upload files to the server. It displays a text box control and a browse button that allows users to select a file to upload to the server.

Properties

  1. FileBytes
  2. FileContent
  3. FileName
  4. SaveAs
  5. PostedFile

The FileUpload control provides the HttpPostedFile class. Its has the following properties:

  1. ContentLength
  2. ContentType
  3. FileName
  4. InputStream
  5. SaveAs

Example

The following shows how to upload a single file.

  1. <b>Upload File:</b>   
  2. <asp:FileUpload ID="FileUpload1" runat="server" />   
The following shows how to upload multiple files.
  1. <b>Upload Multiple File:</b>   
  2. <asp:FileUpload ID="fileuplaod1" runat="server" AllowMultiple="true" Font-Bold="true" />  
We need to set AllowMultiple="true".

Example

The following shows how to upload multiple files from various folders.

DifferentFile.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="differntfolder.aspx.cs" Inherits="differntfolder" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div align="center">  
  12.             <table border="1">  
  13.                 <tr>  
  14.                     <th>Multiple File Upload</th>  
  15.                 </tr>  
  16.                 <tr>  
  17.                     <td></td>  
  18.                 </tr>  
  19.                 <tr>  
  20.                     <td>  
  21.                         <asp:FileUpload ID="fileuplaod1" runat="server" AllowMultiple="true" Font-Bold="true" />  
  22.                     </td>  
  23.                 </tr>  
  24.                 <tr>  
  25.                     <td>  
  26.                         <asp:Button ID="button1" runat="server" Text="upload" OnClick="button1_Click" Width="82px" />  
  27.                     </td>  
  28.                 </tr>  
  29.                 <tr>  
  30.                     <td></td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td>  
  34.                         <asp:Label ID="label1" runat="server" ForeColor="Green" Font-Size="Large" Font-Bold="true"></asp:Label><br />  
  35.                     </td>  
  36.                 </tr>  
  37.                 <tr>  
  38.                     <td></td>  
  39.                 </tr>  
  40.                 <tr>  
  41.                     <td>  
  42.                         <asp:Label ID="labbel2" runat="server" Font-Bold="true" ForeColor="Red" Font-Size="Large"></asp:Label><br />  
  43.                     </td>  
  44.                 </tr>  
  45.                 <tr>  
  46.                     <td>  
  47.                         <asp:Label ID="label3" runat="server" Font-Bold="true" ForeColor="Black" Font-Size="Large"></asp:Label>  
  48.                     </td>  
  49.                 </tr>  
  50.             </table>  
  51.         </div>  
  52.     </form>  
  53. </body>  
  54. </html>  
Code File DifferentFile.aspx.cs
  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.IO;  
  8.   
  9. public partial class differntfolder : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.     }  
  14.     protected void button1_Click(object sender, EventArgs e)  
  15.     {  
  16.         label1.Text = "<b>uploaded file<b/><br/>";  
  17.         labbel2.Text = "<b>not uploaded file<b/><br/>";  
  18.         label1.Visible = true;  
  19.         try  
  20.         {  
  21.             // Check File Prasent or not  
  22.             if (fileuplaod1.HasFiles)  
  23.             {  
  24.                 int filecount = 0;  
  25.                 int fileuploadcount = 0;  
  26.                 //check No of Files Selected  
  27.                 filecount = fileuplaod1.PostedFiles.Count();  
  28.                 if (filecount <= 10)  
  29.                 {  
  30.                     foreach (HttpPostedFile postfiles in fileuplaod1.PostedFiles)  
  31.                     {  
  32.                         //Get The File Extension  
  33.                         string filetype = Path.GetExtension(postfiles.FileName);  
  34.                         if (filetype.ToLower() == ".docx" || filetype.ToLower() == ".pdf" || filetype.ToLower() == ".txt" || filetype.ToLower() == ".doc")  
  35.                         {  
  36.                             //Get The File Size In Bite  
  37.                             double filesize = postfiles.ContentLength;  
  38.                             if (filesize < (1048576))  
  39.                             {  
  40.                                 fileuploadcount++;  
  41.                                 string serverfolder = string.Empty;  
  42.                                 string serverpath = string.Empty;  
  43.                                  // Adding File Into Scecific Folder Depend On his Extension  
  44.                                 switch (filetype)  
  45.                                 {  
  46.                                     case ".doc":  
  47.                                     case ".docx":  
  48.                                         serverfolder = Server.MapPath(@"uplaodfiles\document\");  
  49.                                         //check Folder avlalible or not  
  50.                                         if (!Directory.Exists(serverfolder))  
  51.                                         {  
  52.                                             // create Folder  
  53.                                             Directory.CreateDirectory(serverfolder);  
  54.                                         }  
  55.                                         serverpath = serverfolder + Path.GetFileName(postfiles.FileName);  
  56.                                         fileuplaod1.SaveAs(serverpath);  
  57.                                         label1.Text += "[" + postfiles.FileName + "]- document file uploaded  successfully<br/>";  
  58.                                         break;  
  59.                                     case ".pdf":  
  60.                                         serverfolder = Server.MapPath(@"uplaodfiles\pdf\");  
  61.                                         //check Folder avlalible or not  
  62.                                         if (!Directory.Exists(serverfolder))  
  63.                                         {  
  64.                                             // create Folder  
  65.                                             Directory.CreateDirectory(serverfolder);  
  66.                                         }  
  67.                                         serverpath = serverfolder + Path.GetFileName(postfiles.FileName);  
  68.                                         fileuplaod1.SaveAs(serverpath);  
  69.                                         label1.Text += "[" + postfiles.FileName + "]- pdf file uploaded  successfully<br/>";  
  70.                                         break;  
  71.                                     case ".txt":  
  72.                                         serverfolder = Server.MapPath(@"uplaodfiles\text_document\");  
  73.                                         //check Folder avlalible or not  
  74.                                         if (!Directory.Exists(serverfolder))  
  75.                                         {  
  76.                                             // create Folder  
  77.                                             Directory.CreateDirectory(serverfolder);  
  78.                                         }  
  79.                                         serverpath = serverfolder + Path.GetFileName(postfiles.FileName);  
  80.                                         fileuplaod1.SaveAs(serverpath);  
  81.                                         label1.Text += "[" + postfiles.FileName + "]- text_document file uploaded  successfully <br/>";  
  82.                                         break;  
  83.                                 }  
  84.                             }  
  85.                             else  
  86.                             {  
  87.                                 labbel2.Text += "[" + postfiles.FileName + "]- files not uploded size is greater then(1)MB.<br/>Your File Size is(" + (filesize / (1024 * 1034)) + ") MB </br>";  
  88.                             }  
  89.                         }  
  90.                         else  
  91.                         {  
  92.                             labbel2.Text += "[" + postfiles.FileName + "]- file type must be .doc or pdf and other<br/>";  
  93.                         }  
  94.                     }  
  95.                 }  
  96.                 else  
  97.                 {  
  98.                     label1.Visible = false;  
  99.                     labbel2.Text = "you are select(" + filecount + ")files <br/>";  
  100.                     labbel2.Text += "please select Maximum five(10) files !!!";  
  101.                 }  
  102.                 label3.Visible = true;  
  103.                 label3.Text = "ToTal File =(" + filecount + ")<br/> Uploded file =(" + fileuploadcount + ")<br/> Not Uploaded=(" + (filecount - fileuploadcount) + ")";  
  104.             }  
  105.             else  
  106.             {  
  107.                 label1.Visible = false;  
  108.                 label3.Visible = false;  
  109.                 labbel2.Text = "<b>please select the file for upload !!!</b></br>";  
  110.             }  
  111.         }  
  112.         catch (Exception ex)  
  113.         {  
  114.             labbel2.Text = ex.Message;  
  115.         }  
  116.     }  
  117. }  
Output
  • If you are selecting more than 5 files.

    select More than Five file

  • If the file is not selected.

    If File is not selected
  • After uploading multiple files.

    After Uploading Multiple Files

  • After uploading files.

    After Uploading files