Table of contents
1.
Introduction
2.
Multiple File upload in ASP.NET
3.
Attributes and properties for multiple file upload
4.
The syntax for multiple file upload
5.
Coding example for multiple file upload in ASP.NET
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

ASP.NET Upload Multiple Files

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Some web applications have forms that require you to upload files. These files could be anything ranging from text files to PDFs. You can upload multiple image files from the gallery on social media websites. Similarly, ASP.NET has properties and attributes that allow uploading multiple files at once. 
In this article, we will dive into the topic of Multiple Fileupload ASP.NET. Let’s start learning!

Multiple File upload in ASP.NET

When it comes to uploading files of various types, Microsoft's.Net has come a long way. The FileUpload server-side control was added for the first time in Asp.Net version 2.0. This has made the process of uploading files in Asp.Net a lot easier.
ASP.NET 4.5 introduces many new features that improve the functionality of earlier versions. The FileUpload control in ASP.NET 4.5 expands its capability by allowing multiple files to be uploaded simultaneously.

Attributes and properties for multiple file upload

Attributes and properties that come with the ASP.NET 4.5 FileUplaod control are:

  • AllowMultiple: To support multiple file uploads, set AllowMulitple="true" other than false.
  • HasFiles: It Checks whether FileUpload control has multiple files or not.
  • PostedFiles: It is used to get all the files from the FileUpload control in the iteration.
     

The HasFiles and PostedFiles properties will help work with the multiple file uploads in C#.

The syntax for multiple file upload

 On the ASPX page, declare a FileUpload control:

<asp:FileUpload runat="server" ID="UploadFiles" AllowMultiple="true" />

Setting the AllowMultiple attribute to "true" is necessary to enable the multiple file upload feature.
The above code will be rendered in the browser as the following HTML:

<input type="file" multiple="multiple" name="UploadFiles" id="UploadFiles" />

Coding example for multiple file upload in ASP.NET

Given below is an example code for multiple file uploads. 

ASPX file:
In the following aspx file, we have a FileUpload control with AllowMultiple as true so that multiple files can be uploaded simultaneously. There is upload button that uploads and saves files by calling the uploadFileOnClick method on click. The label will display the names of the files that are uploaded. 

<!DOCTYPE html>    
    <head runat="server">    
        <title></title>    
    </head>    
    <body>    
        <div>  
            <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />  
            <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFileOnClick" />  
            <asp:Label ID="uploadedfileslist" runat="server" />  
         </div>    
    </body>    
</html>    

 

CS file:
When the uploadFileOnClick method is called, it first checks if the FileUpload control with ID UploadImages has multiple files using HasFiles property. If it has files, using foreach loop and PostFiles property, we iterate over all the files uploaded and save them in the Images folder and display each file one below the other.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace UploadMultipleExample  
{  
    public partial class UploadMultipleFilesExample : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
        }  
        protected void uploadFileOnClick(object sender, EventArgs e)  
        {  
            if (UploadImages.HasFiles) {  
                foreach(HttpPostedFile uploadedFile in UploadImages.PostedFiles) {  
                uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), uploadedFile.FileName));  
                uploadedfileslist.Text += String.Format("{0}<br />", uploadedFile.FileName);  
                }  
            }  
        }  
    }  
}  

Output:

Click Choose Files button to select multiple files

After selecting multiple files and uploading them simultaneously

The files saved in the Images folder in the application

FAQs

  1. Which browsers support multiple file uploads in ASP.NET?
    Multiple file uploads feature in ASP.NET is only available in browsers supporting HTML5, i.e., Internet Explorer (IE10) or higher, Firefox, and Chrome.
     
  2. What does HttpPostedFile do?
    The HttpPostedFile class has properties and methods for getting information on a specific file and reading and saving it.
     
  3. What does Server.MapPath() do?
    The MapPath method is used to define a relative virtual path for a physical directory on the server. 
     
  4. What does file.SaveAs() do?
    The SaveAs method stores the contents of an uploaded file on the Web server to a specified destination. After the user picks a file to upload, the FileUpload control does not automatically save it to the server. To allow the user to send the requested file, you must explicitly provide a control or mechanism.
     
  5. What is System.IO.Path.Combine() used for?
    This method is used to merge the path of the file with another string.

Key Takeaways

From this article, we learned about Multiple Fileupload in ASP.Net. We saw how it is practically implemented through code. 

Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form.

But this is not enough; you need something extra to excel in web development truly. If you want to learn more about web development, you can read our articles or take our highly curated Web Development course.

Live masterclass