multiple file upload asp.net + javascript

Fileupload.aspx

<script src=”Scripts/jquery-1.4.1.min.js” type=”text/javascript”></script>
<script src=”Scripts/jquery.MultiFile.pack.js” type=”text/javascript”></script>
<div><%– accept attribute can be used like accept=”png|jpg”–%>
Multiple File Upload<br />
<asp:FileUpload ID=”FileUpload10″ runat=”server” accept=”” />
<asp:Button ID=”Button3″ runat=”server” Text=”Submit” OnClick=”jQueryUploadFiles” />

<br />
<asp:Label ID=”lblMessage” runat=”server” EnableViewState=”false” ForeColor=”Green” />
<br />
<asp:Label ID=”lblError” runat=”server” EnableViewState=”false” ForeColor=”Red” />
</div>

Fileupload.cs

/// <summary>
/// Multiple file upload using ASP.NET using jQuery file selection mechanism
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void jQueryUploadFiles(object sender, EventArgs e)
{
CreateDirectory();
FileUploadUsingJQuerySelectionMethod();
}

/// <summary>
/// Create directory to upload a file.
/// </summary>
private void CreateDirectory()
{
if (!Directory.Exists(ConfigurationManager.AppSettings[“FilePath”]))
{
Directory.CreateDirectory(ConfigurationManager.AppSettings[“FilePath”]);
}
}

/// <summary>
/// file upload using jQuery file selection mechanism
/// </summary>
private void FileUploadUsingJQuerySelectionMethod()
{
// check if file has been selected
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
if (file.ContentLength > 0)
{
string path = ConfigurationManager.AppSettings[“FilePath”];
string fileName = Path.GetFileName(file.FileName);

// now save the file to the disk
file.SaveAs(path + fileName);

lblMessage.Text += “File : <b>” + fileName + “</b> uploaded successfully !<br />”;
}
}
}

scripts needed download link:

https://rapidshare.com/files/3466276923/Scripts.rar

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s