Hi Ramu
Here is the code which helps you to upload multiple file by using ASP.NET.
Thanks
Thiru ~
MS Techi...
Uploading Multiple Files at the Same Time
Now let's take a look at how to upload multiple files to the server from a single page.
No built-in capabilities in the Microsoft .NET Framework enable you to upload multiple files from a single ASP.NET page. With a little work, however, you can easily accomplish this task.
One trick is to import the System.IO class into your ASP.NET page, and to then use the HttpFileCollection class to capture all the files that are sent in with the Request object. This approach enables you to upload as many files as you want from a single page.
For this example, you can build an ASP.NET page that has four file-input boxes and one Submit button. After the user clicks the Submit button and the files are posted to the server, the code behind takes the files and saves them to a specific location on the server. After the files are saved, the file information that was posted is displayed in the ASP.NET page (see Listing 3).
Listing 3: Uploading multiple files to the server
VB
<%@ Import Namespace="System.IO" %>
<%@ Page Language="VB" %>
<script runat="server">
Sub SubmitButton_Click(Source As Object, e As EventArgs)
Dim filepath As String = "C:\Uploads"
Dim uploadedFiles As HttpFileCollection = Request.Files
Dim i As Integer = 0
Do Until i = uploadedFiles.Count
Dim userPostedFile As HttpPostedFile = uploadedFiles(i)
Try
If (userPostedFile.ContentLength > 0) Then
Span1.InnerHtml += "<u>File #" & (i+1) & "</u><br>"
Span1.InnerHtml += "File Content Type: " & _
userPostedFile.ContentType & "<br>"
Span1.InnerHtml += "File Size: " & _
userPostedFile.ContentLength & "kb<br>"
Span1.InnerHtml += "File Name: " & _
userPostedFile.FileName & "<br>"
userPostedFile.SaveAs(filepath & "\" & _
Path.GetFileName(userPostedFile.FileName))
Span1.InnerHtml += "Location where saved: " & _
filepath & "\" & _
Path.GetFileName(userPostedFile.FileName) & _
"<p>"
End If
Catch ex As Exception
Span1.InnerHtml += "Error:<br>" & ex.Message
End Try
i += 1
Loop
End Sub
</script>
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" runat="server">
<p>
Select File1:<br />
<input id="File1" type="file" runat="Server" />
<br />
Select File2:<br />
<input id="File2" type="file" runat="Server" />
<br />
Select File3:<br />
<input id="File3" type="file" runat="Server" />
<br />
Select File4:<br />
<input id="File4" type="file" runat="Server" />
</p>
<p>
<input id="Submit1" type="submit" value="Upload Files"
runat="Server" />
<br />
</p>
<span id="Span1" runat="Server"></span>
</form>
</body>
</html>
C#
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" %>
<script runat="server">
protected void SubmitButton_Click(Object sender, EventArgs e){
string filepath = "C:\\Uploads";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
if (userPostedFile.ContentLength > 0 )
{
Span1.InnerHtml += "<u>File #" + (i+1) +
"</u><br>";
Span1.InnerHtml += "File Content Type: " +
userPostedFile.ContentType + "<br>";
Span1.InnerHtml += "File Size: " +
userPostedFile.ContentLength + "kb<br>";
Span1.InnerHtml += "File Name: " +
userPostedFile.FileName + "<br>";
userPostedFile.SaveAs(filepath + "\\" +
Path.GetFileName(userPostedFile.FileName));
Span1.InnerHtml += "Location where saved: " +
filepath + "\\" +
Path.GetFileName(userPostedFile.FileName) +
"<p>";
}
}
catch (Exception Ex)
{
Span1.InnerText += "Error: <br>" + Ex.Message;
}
}
}
</script>
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" runat="server">
<p>
Select File1:<br />
<input id="File1" type="file" runat="Server" />
<br />
Select File2:<br />
<input id="File2" type="file" runat="Server" />
<br />
Select File3:<br />
<input id="File3" type="file" runat="Server" />
<br />
Select File4:<br />
<input id="File4" type="file" runat="Server" />
</p>
<p>
<input id="Submit1" type="submit" value="Upload Files"
runat="Server" />
<br />
</p>
<span id="Span1" runat="Server"></span>
</form>
</body>
</html>
The end user can select up to four files and click the Upload Files button, which initializes the SubmitButton_Click event. Using the HttpFileCollection class with the Request.Files property lets you gain control over all the files that are uploaded from the page. When the files are in this state, you can do whatever you want with them. In this case, the files' properties are examined and written to the screen. In the end, the files are saved to the Uploads folder in the root directory of the server. The result of this action is illustrated in Figure 5.