Uploading Multiple file in asp.net (c#)

Bageshkumarbagi
Posted by Bageshkumarbagi under ASP.NET category on | Points: 40 | Views : 5028
HTML CODE
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>To Upload Multiple File in ASP.NET </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Upload Multiple Files in Asp.net Using C# code</h1>
<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" /> </div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload Files" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

===========C# code===========
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpFileCollection multipleFiles = Request.Files;
for (int fileCount = 0; fileCount < multipleFiles.Count; fileCount++)
{
HttpPostedFile uploadedFile = multipleFiles[fileCount];
string fileName = Path.GetFileName(uploadedFile.FileName);
if (uploadedFile.ContentLength > 0 )
{
uploadedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
Label1.Text += fileName + "Saved <BR>";
}
}

}
}
}

=====================
You need to create a Folder in Solutation Explorer which name is " Files "

Comments or Responses

Login to post response