Wednesday, 16 April 2014

Upload multiple files using ASP.NET


In this article I will explain how to upload multiple file in ASP.Net using JQuery multiple upload plugin.

In this scenario we need to download JQuery multiple upload plugin for multiple file upload functionality. Click below download link for Plugin download.


Right click the website, Add new folder with the name as Upload. We save uploaded files in this folder.

Example :



Below is the designing code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Upload multiple files Using JQuery in asp.net</title>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="jquery.MultiFile.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<table align="center">
<tr>
<td colspan="2" style="font-size: large;"><b><u>Upload multiple files Using JQuery in asp.net</u></b>
</td>
</tr>
<tr>
<td colspan="2" height="20px"></td>
</tr>
<tr>
<td>Select File :</td>
<td>
<asp:FileUpload ID="fuMultipleFiles" runat="server" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" /></td> 
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>

Code behind

using System;
using System.IO;
using System.Web;

public partial class MultipleFiles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpFileCollection MultiplefileCollection = Request.Files;
for (int i = 0; i < MultiplefileCollection.Count; i++)
{
HttpPostedFile uploadfile = MultiplefileCollection[i];
string fileName = Path.GetFileName(uploadfile.FileName);
if (uploadfile.ContentLength > 0)
{
uploadfile.SaveAs(Server.MapPath("~/Upload/") + fileName);
lblMessage.Text += fileName + "Saved Successfully<br>";
}
}
}
}


No comments:

Post a Comment