Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to upload multiple files in ASP.NET

Author: Rachel Nicole

Code snippet in this article will show how to upload multiple files at a time in ASP.NET.

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Right click on website > New Folder (Name the folder as “MyFolder”)
  6. Write code below in Default.aspx page

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


     We are using three FileUpload controls to upload three files at a time.
     
  7. Now write code below in Button click event in code behind file. Don’t forget to add “System.IO” namespace at the top as we are going to work with files.

    C#

    using System.IO; 
     
    protected void Button1_Click(object sender, EventArgs e) {
        HttpFileCollection fileCollection = Request.Files;
        for (int i = 0; i < fileCollection.Count; i++) {
            HttpPostedFile postedFile = fileCollection[i];
            if (postedFile.ContentLength > 0) {
                string filePath = Server.MapPath("MyFolder") + "\\" + Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(filePath);
                Label1.Text = "Files uploaded successfully";
            }
        }
     
    }
     
    VB.NET

    Imports System.IO

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FileCollection As HttpFileCollection = Request.Files
        For i As Integer = 0 To FileCollection.Count - 1
            Dim postedFile As HttpPostedFile = FileCollection(i)
            If postedFile.ContentLength > 0 Then
                Dim filePath As String = Server.MapPath("MyFolder") + "\\" + Path.GetFileName(postedFile.FileName)
                postedFile.SaveAs(filePath)
                Label1.Text = "Files uploaded successfully"
            End If
        Next i
    End Sub
     

    “HttpFileCollection” class is used to retrieve posted files. We loop through the file and get the each posted file by using “HttpPostedFile” class. We get the file name by using FileName() method of “HttpPostedFile” class. At the end we saved our files in the specified folder by using SaveAs() method of “HttpPostedFile” class.
     
  8. Build website and view in browser. Select files and click on Button.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to read data from excel file in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net