Content feed Comments Feed

The Official ASATO Site

Hi, welcome to my blog. ASP,asp.net,Health,Javascript,JQUERY

How to Upload multiple files in asp.net

Posted by admin On March - 23 - 2010

In asp.net 2.0, if you want to upload a file, you can use FileUpload server control in your in your applications.like this:
ASPX code:

?View Code CSHARP
1
2
3
4
<div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"  Text="Upload--File" />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>

ASPX.CS code:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
            try
            {
                FileUpload1.SaveAs("G:\\Uploads\\" +  FileUpload1.FileName);
                Label1.Text = "File name: " +
                     FileUpload1.PostedFile.FileName + "<br>" +
                     FileUpload1.PostedFile.ContentLength + " kb<br>" +
                     "Content type: " +
                     FileUpload1.PostedFile.ContentType;
            }
            catch (Exception ex)
            {
                Label1.Text = "ERROR: " + ex.Message.ToString();
            }
        else
        {
            Label1.Text = "You have not specified a file.";
        }
    }

But, if we want to Upload multiple files, how can we do ?
aspx:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Upload multiple files</title>
 
  <script type="text/javascript">
    function addFiles() {
      var div = document.createElement("div");
      var f = document.createElement("input");
      f.setAttribute("type", "file")
      f.setAttribute("name", "File")
      f.setAttribute("size", "40")
      div.appendChild(f)
      var d = document.createElement("input");
      d.setAttribute("type", "button")
      d.setAttribute("onclick", "deteFile(this)");
      d.setAttribute("value", "remove")
      div.appendChild(d)
      document.getElementById("_container").appendChild(div);
    }
 
    function deteFile(o) {
      while (o.tagName != "DIV") o = o.parentNode;
      o.parentNode.removeChild(o);
    }
  </script>
</head>
<body>
  <form id="form1" runat="server" method="post" enctype="multipart/form-data">
  <h3>Upload multiple files</h3>
   <div id="_container">
    <input type="file" size="50" name="File" />
  </div>
  <div>
    <input type="button" value="Add" onclick="addFiles()" />
  </div>
  <div style="padding:10px 0">
    <asp:Button runat="server" Text="Upload files" ID="UploadButton" onclick="UploadButton_Click"></asp:Button>
  </div>
  <div>
    <asp:Label ID="Label1" runat="server"  Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
  </div>
  </form>
</body>
</html>

aspx.cs:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
protected void UploadButton_Click(object sender, EventArgs e)
    {
      ///foreach File form
      HttpFileCollection files = HttpContext.Current.Request.Files;
      strMsg.Append("file list is:<hr color='red'/>");
      try
      {
        for (int iFile = 0; iFile < files.Count; iFile++)
        {
          HttpPostedFile postedFile = files[iFile];
          string fileName, fileExtension;
          fileName = System.IO.Path.GetFileName(postedFile.FileName);
          if (fileName != "")
          {
            fileExtension = System.IO.Path.GetExtension(fileName);
            strMsg.Append("type:" + postedFile.ContentType.ToString() + "<br>");
            strMsg.Append("client dress:" + postedFile.FileName + "<br>");
            strMsg.Append("file name:" + fileName + "<br>");
            strMsg.Append("fileExtension :" + fileExtension + "<br><hr>");
            postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("photoshops/") + fileName);
          }
        }
        Label1.Text = strMsg.ToString();
      }
      catch (System.Exception Ex)
      {
        Label1.Text = Ex.Message;
      }
    }

Note: the form must set enctype=”multipart/form-data”

Leave a Reply