Subscribe Now!

Enter your email address:

Friday, January 21, 2011

How To Post Multiple Files From File Upload Control.


We useFile Upload control when we want to upload any kind of file may be text file, doc file or else... So today i share a program with you how to upload multiple file with file upload controls on the same page.


The Following code for Default.aspx Page [Source] 

<form id="form1" runat="server">
<div>

<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:FileUpload ID="FileUpload2" runat="server" />
<br />
<br />
<asp:FileUpload ID="FileUpload3" runat="server" />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Upload Files" Width="125px" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />


</div>
</form>


The following code for Default.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
String filepath = Server.MapPath(".");

HttpFileCollection filecollection = Request.Files;

for (int i = 0; i < filecollection.Count; i++)
{
HttpPostedFile postedfiles = filecollection[i];

try
{
if(postedfiles.ContentLength >0)
{
Label1.Text += postedfiles.ContentType + "
";
Label1.Text += postedfiles.ContentLength + "
";
Label1.Text += System.IO.Path.GetFileName(postedfiles.FileName) +"
";
postedfiles.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(postedfiles.FileName));
}
}
catch(Exception exp)
{
Response.Write(exp.Message);
}

}

}
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...