How to upload multiple files in ASP.NET ?

 Posted by Vivek.Ramapuram on 5/19/2015 | Category: ASP.NET Interview questions | Views: 764 | Points: 40
Answer:

In ASP.NET simultaneously on the server we can use multiple files to upload file

Example :
ASPX PAGE :
 Upload files<br />

<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:FileUpload ID="FileUpload2" runat="server" /><br />
<asp:FileUpload ID="FileUpload3" runat="server" /><br />
<asp:FileUpload ID="FileUpload4" runat="server" /><br />
<p><asp:Button ID="btnUpload" runat="server" Text="Upload Multiple Files" OnClick="UploadMultipleFiles" /></p>


Code behind :
protected void   UploadMultipleFiles(object sender, EventArgs e)

{
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];

string path = Server.MapPath("~/");
string fileName = file.FileName;
file.SaveAs(path + fileName);
Response.Write(fileName + " uploaded successfully ! <br />");
}
}


Asked In: While Learning | Alert Moderator 

Comments or Responses

Login to post response