In this article I will explain with an example, how to upload multiple files with ASP.Net 4.5 FileUpload control in Visual Studio 2012 and Visual Studio 2013 using C# and VB.Net.
The HTML5 FileUpload control supports selection of multiple files at once and all the selected files are uploaded together.
 
 

HTML Markup

The HTML Markup consists of:
FileUpload – For selecting multiple file.
The FileUpload control has been set with AllowMultiple property to True.
Button – For upload selected file.
The Button has been assigned with an OnClick event handler.
Label – For display success message after uploading file.
<asp:FileUpload ID="fuUpload" runat="server" AllowMultiple="true" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="OnUpload" />
<hr />
<asp:Label ID="lblMessage" runat="server" ForeColor="Green"></asp:Label>
 
 

Namespaces

You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 

Uploading multiple files with ASP.Net 4.5 FileUpload

When the Upload Button is clicked, following event handler is executed.
Inside this event handler, first a check is performed whether the Folder (Directory) exists. If it does not then, the Folder (Directory) is created.
Next, a loop is executed over the FileUpload PostedFiles property which holds the uploaded files.
Inside the loop, the names of the files are extracted and then the files are stored in Folder (Directory).
Finally, the success message is displayed using the Label control.
C#
protected void OnUpload(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Files/");
 
    //Check whether Directory (Folder) exists.
    if (!Directory.Exists(folderPath))
    {
        //If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath);
    }
 
    foreach (HttpPostedFile postedFile in fuUpload.PostedFiles)
    {
        //Save the File to the Directory (Folder).
        postedFile.SaveAs(folderPath + Path.GetFileName(postedFile.FileName));
    }
 
    //Display the success message.
    lblMessage.Text = string.Format("{0} files have been uploaded successfully.", fuUpload.PostedFiles.Count);
}
 
VB.Net
Protected Sub OnUpload(ByVal sender As ObjectByVal e As EventArgs)
    Dim folderPath As String = Server.MapPath("~/Files/")
 
    'Check whether Directory (Folder) exists.
    If Not Directory.Exists(folderPath) Then
        'If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath)
    End If
 
    For Each postedFile As HttpPostedFile In fuUpload.PostedFiles
        'Save the File to the Directory (Folder).
        postedFile.SaveAs(folderPath & Path.GetFileName(postedFile.FileName))
    Next
 
    'Display the success message.
    lblMessage.Text = String.Format("{0} files have been uploaded successfully.", fuUpload.PostedFiles.Count)
End Sub
 
 

Screenshot

Upload multiple files with ASP.Net 4.5 FileUpload control in Visual Studio 2012 and 2013
 
 

Browser Compatibility

The above code has been tested in the following browsers.
Microsoft Edge   FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Downloads