C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET Upload Multiple FilesASP.NET FileUpload control provides AllowMultiple property to upload multiple files to the server. This property takes either true or false value. The <asp:FileUpload> tag is used to create a browse button that allows us to upload file. Let?s create an example to upload multiple files. ASP.NET Upload Multiple Files Example
This example contains the following files. // UploadMultipleFilesExample.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="UploadMultipleFilesExample.aspx.cs" Inherits="UploadMultipleExample.UploadMultipleFilesExample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Upload Multiple Files</h3>
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" />
</p>
</form>
<p>
<asp:Label runat="server" ID="FileUploadStatus"></asp:Label>
</p>
</body>
</html>
// UploadMultipleFilesExample.aspx.cs
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 Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
{
var count = 0;
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
uploadedFile.SaveAs(SaveLocation);
count++;
}
catch (Exception ex)
{
FileUploadStatus.Text = "Error: " + ex.Message;
}
}
if (count > 0)
{
FileUploadStatus.Text = count + " files has been uploaded.";
}
}
else
{
FileUploadStatus.Text = "Please select a file to upload.";
}
}
}
}
Output:
Selecting 2 files to upload ![]()
See, initially, the upload folder is empty.
Uploading files to the server.
Now, look at the upload folder. It contains uploaded two files.
Next TopicASP.NET Download File
|