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#
VB.Net
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 Object, ByVal 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
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Downloads