【问题标题】:How to choose multiple files using File Upload Control?如何使用文件上传控件选择多个文件?
【发布时间】:2013-06-30 18:50:25
【问题描述】:

我有一个文件上传控件。现在点击它,我想选择多个文件。

我该怎么做?

【问题讨论】:

  • @Basic - 具有讽刺意味。 1 年后,我用谷歌搜索了“asp.net 文件上传多个”,您的评论意味着这是最重要的。
  • @Rudi 对不起!至少我给了三个很好的链接...

标签: asp.net file-upload


【解决方案1】:

.NET 4.5 及更高版本中的FileUpload.AllowMultiple 属性将允许您控制选择多个文件。

<asp:FileUpload ID="fileImages" AllowMultiple="true" runat="server" />

.NET 4 及以下

 <asp:FileUpload ID="fileImages" Multiple="Multiple" runat="server" />

在回发时,您可以:

 Dim flImages As HttpFileCollection = Request.Files                   
 For Each key As String In flImages.Keys
    Dim flfile As HttpPostedFile = flImages(key)
    flfile.SaveAs(yourpath & flfile.FileName)
 Next

【讨论】:

  • flBanner 是什么?
  • for each key as String 循环对我不起作用(它一遍又一遍地列出相同的文件名),但使用基于索引的循环和For i = 0 To uploadedFiles.Count - 1 可以。
  • 当我浏览每个文件时,我可以查看文件的内容吗?顺便说一句,我正在 C# 中尝试这个
【解决方案2】:

这里是如何在 asp.net 中选择和上传多个文件的完整示例 使用文件上传控制....

将此代码写入 .aspx 文件..

<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
    <input type="file" id="myfile" multiple="multiple" name="myfile" runat="server" size="100" />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <br />
    <asp:Label ID="Span1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

然后在 .aspx.cs 文件中编写此代码..

   protected void Button1_Click(object sender,EventArgs e) {
          string filepath = Server.MapPath("\\Upload");
          HttpFileCollection uploadedFiles = Request.Files;
          Span1.Text = string.Empty;

          for(int i = 0;i < uploadedFiles.Count;i++) {
              HttpPostedFile userPostedFile = uploadedFiles[i];

              try {
                  if (userPostedFile.ContentLength > 0) {
                     Span1.Text += "<u>File #" + (i + 1) +  "</u><br>";
                     Span1.Text += "File Content Type: " +  userPostedFile.ContentType      + "<br>";
                     Span1.Text += "File Size: " + userPostedFile.ContentLength           + "kb<br>";
                     Span1.Text += "File Name: " + userPostedFile.FileName + "<br>";

                     userPostedFile.SaveAs(filepath + "\\" +    Path.GetFileName(userPostedFile.FileName));                  
                     Span1.Text += "Location where saved: " +   filepath + "\\" +   Path.GetFileName(userPostedFile.FileName) + "<p>";
                  }
              } catch(Exception Ex) {
                  Span1.Text += "Error: <br>" + Ex.Message;
              }
           }
        }
    }

你来了……你的多文件上传控制已经准备好了……祝你有快乐的一天。

【讨论】:

  • 这段代码非常感谢。但是 HttpPostedFile.ContentLength 以字节表示,而不是千比特...
【解决方案3】:
        aspx code

            <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
            <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick ="UploadMultipleFiles" accept ="image/gif, image/jpeg" />
            <hr />
            <asp:Label ID="lblSuccess" runat="server" ForeColor ="Green" />


    Code Behind:

protected void UploadMultipleFiles(object sender, EventArgs e)
{
     foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
     {
          string fileName = Path.GetFileName(postedFile.FileName);
          postedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
     }
     lblSuccess.Text = string.Format("{0} files have been uploaded successfully.", FileUpload1.PostedFiles.Count);
}

【讨论】:

    【解决方案4】:

    第 1 步:添加

    <asp:FileUpload runat="server" id="fileUpload1" Multiple="Multiple">
        </asp:FileUpload>
    

    第 2 步:添加

    Protected Sub uploadBtn_Click(sender As Object, e As System.EventArgs) Handles uploadBtn.Click
        Dim ImageFiles As HttpFileCollection = Request.Files
        For i As Integer = 0 To ImageFiles.Count - 1
            Dim file As HttpPostedFile = ImageFiles(i)
            file.SaveAs(Server.MapPath("Uploads/") & ImageFiles(i).FileName)
        Next
    
    End Sub
    

    【讨论】:

      【解决方案5】:

      您可以使用其他选项,这些控件具有多个上传选项,并且这些控件还支持 Ajax

      1) Flajxian
      2) Valums
      3)Subgurim FileUpload

      【讨论】:

        【解决方案6】:

        default.aspx 代码

        <asp:FileUpload runat="server" id="fileUpload1" Multiple="Multiple">
                </asp:FileUpload>
        <asp:Button runat="server" Text="Upload Files" id="uploadBtn"/>
        

        default.aspx.vb

        Protected Sub uploadBtn_Click(sender As Object, e As System.EventArgs) Handles uploadBtn.Click
            Dim ImageFiles As HttpFileCollection = Request.Files
            For i As Integer = 0 To ImageFiles.Count - 1
            Dim file As HttpPostedFile = ImageFiles(i)
                file.SaveAs(Server.MapPath("Uploads/") & file.FileName) 
            Next       
        End Sub
        

        【讨论】:

          【解决方案7】:

          要添加多个文件,请使用以下代码

          <html xmlns="http://www.w3.org/1999/xhtml">
          <head runat="server">
              <title></title>
              <style type="text/css">
              .fileUpload{
              width:255px;    
              font-size:11px;
              color:#000000;
              border:solid;
              border-width:1px;
              border-color:#7f9db9;    
              height:17px;
              }
              </style>
          </head>
          <body>
              <form id="form1" runat="server">
              <div>
              <div id="fileUploadarea"><asp:FileUpload ID="fuPuzzleImage" runat="server" CssClass="fileUpload" /><br /></div><br />
              <div><input style="display:block;" id="btnAddMoreFiles" type="button" value="Add more images" onclick="AddMoreImages();" /><br />
                  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
                  </div>
              </div>
              <script language="javascript" type="text/javascript">
                  function AddMoreImages() {
                      if (!document.getElementById && !document.createElement)
                          return false;
                      var fileUploadarea = document.getElementById("fileUploadarea");
                      if (!fileUploadarea)
                          return false;
                      var newLine = document.createElement("br");
                      fileUploadarea.appendChild(newLine);
                      var newFile = document.createElement("input");
                      newFile.type = "file";
                      newFile.setAttribute("class", "fileUpload");
          
                      if (!AddMoreImages.lastAssignedId)
                          AddMoreImages.lastAssignedId = 100;
                      newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
                      newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
                      var div = document.createElement("div");
                      div.appendChild(newFile);
                      div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
                      fileUploadarea.appendChild(div);
                      AddMoreImages.lastAssignedId++;
                  }
          
              </script>
              </form>
          </body>
          </html>
          

          服务器端代码:

          try
          {
              HttpFileCollection hfc = Request.Files;
              for (int i = 0; i < hfc.Count; i++)
              {
                  HttpPostedFile hpf = hfc[i];
                  if (hpf.ContentLength > 0)
                  {                        
                      hpf.SaveAs(Server.MapPath("~/uploads/") +System.IO.Path.GetFileName(hpf.FileName);                        
                  }
              }
          }
          catch (Exception)
          {
              throw;
          }
          

          【讨论】:

            【解决方案8】:

            .NET 4.5 及更高版本中的 FileUpload.AllowMultiple 属性将允许您控制选择多个文件。

            低于 4.5 和 4.0(vs 2010) 我们可以使用 jquery 在单个控件中上传多个文件,使用 2 个 js 文件: http://code.jquery.com/jquery-1.8.2.jshttp://code.google.com/p/jquery-multifile-plugin/

            在aspx文件上传标签中,添加class="multi"

            <asp:FileUpload ID="FileUpload1" class="multi" runat="server" />
            

            如果您想要工作示例,请转到link 下载示例。

            【讨论】:

              【解决方案9】:

              你可以试试下面的代码:

               Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                  Try
                      Dim j As Integer = 0
                      Dim hfc As HttpFileCollection = Request.Files
                      Dim PathName As String
                      For i As Integer = 0 To hfc.Count - 1
                          Dim hpf As HttpPostedFile = hfc(i)
              
                          If hpf.ContentLength > 0 Then
                              hpf.SaveAs(Server.MapPath("~/E:\") & System.IO.Path.GetFileName(hpf.FileName))
                              PathName = Server.MapPath(hpf.FileName)
              
                              If j < hfc.Count Then
                                  Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
                                  Dim sqlquery As String
                                  sqlquery = "Insert_proc"
                                  Dim con As New SqlConnection(strConnString)
                                  Dim cmd As New SqlCommand(sqlquery, con)
              
                                  cmd.CommandType = CommandType.StoredProcedure
              
                                  cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = PathName
                                  j = j + 1
              
                                  Try
                                      con.Open()
                                      cmd.ExecuteNonQuery()
              
              
                                  Catch ex As Exception
                                      Throw ex
                                  Finally
                                      con.Close()
                                      con.Dispose()
              
                                  End Try
                                  If j = hfc.Count Then
                                      Exit Sub
                                  End If
                              End If
                          End If
                      Next
              
                  Catch generatedExceptionName As Exception
              
                      Throw
                  End Try
              End Sub
              

              【讨论】:

                【解决方案10】:

                如果您使用的是 .Net 4.5(在较低版本中不支持此功能),我刚刚遇到了这个非常简单的解决方案,您可以使用 jQuery 使事情变得非常简单和轻松。

                Uploading Multiple Files Using jQuery and Generic Handler in ASP.Net 4.5

                当然还有经典 ASP 的商业版本,可以在ASP Uploader找到

                【讨论】:

                  【解决方案11】:

                  为什么不使用 flash & javascript 上传控件?

                  http://www.uploadify.com/demos/

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-08-10
                    • 2020-02-04
                    • 2018-09-24
                    • 1970-01-01
                    • 2011-07-28
                    相关资源
                    最近更新 更多