PROWAREtech

articles » archived » asp-net » upload-multiple-files

ASP.NET: Upload Multiple Files to the Server

How to upload files to the server from the browser using Web Forms (.NET Framework).

To upload multiple files to an ASP.NET server add the multiple attribute to the file type of the input control. See File Upload article for more information or to upload files using AJAX and MVC see this article.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
	if (Page.IsPostBack)
	{
		string filepath = Server.MapPath("/");
		HttpFileCollection files = Request.Files;
		for (int i = 0; i < files.Count; i++)
		{
			HttpPostedFile file = files[i];
			try
			{
				if (file.ContentLength > 0)
				{
					Label1.InnerHtml += "File #" + (i + 1).ToString() + "<br />";
					Label1.InnerHtml += "File Content Type: " + file.ContentType + "<br />";
					Label1.InnerHtml += "File Size: " + file.ContentLength + " bytes<br />";
					Label1.InnerHtml += "File Name: " + file.FileName + "<br />";
					file.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(file.FileName));
				}
			}
			catch (Exception ex)
			{
				Label1.InnerHtml += "<br />" + ex.Message + "<br />";
			}
		}
	}
}
</script>
<html>
<head runat="server">
	<title></title>
</head>
<body>
	<form id="form1" enctype="multipart/form-data" runat="server">
	<div>
	<input type="file" name="images" accept="image/jpeg" multiple required />
	<input type="submit" value="upload" />
	</div>
	</form>
	<div id="Label1" runat="server"></div>
</body>
</html>

Change the web.config file to allow larger files to be uploaded (maxRequestLength) and to allow more time (executionTimeout) for them to be uploaded.

<system.web>
	<httpRuntime executionTimeout="600" maxRequestLength="20480" requestValidationMode="2.0"/>
</system.web>
<system.webServer>
<security>
	<requestFiltering>
	<requestLimits maxAllowedContentLength="102400000"/>
	</requestFiltering>
</security>
</system.webServer>


This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE