2013-07-03 168 views
27

我有一個文件上傳control.Now點擊,我想選擇多個文件。如何使用文件上傳控件選擇多個文件?

我該怎麼做?

+4

我會從谷歌搜索開始'asp.net文件上傳多個'或看看[這裏](http://stackoverflow.com/questions/5508505/multi-file-upload-using-c-sharp-on-asp - 網絡4-0環境),[這裏](http://stackoverflow.com/questions/1222330/multiple-file-selection-for-uploading-in-asp-net)或[這裏](http://stackoverflow.com/questions/3550154/select-multiple-files-to-upload-in-asp-net-preferably-without-flash)在堆棧溢出 – Basic

+15

@Basic - 諷刺。 1年後,我已經使用'asp.net文件上傳倍數'來搜索,並且您的評論意味着這將出現在最前面。 –

+1

@Rudi對不起!至少我給了三個很好的鏈接... – Basic

回答

39

FileUpload.AllowMultiple屬性.NET 4.5及更高版本將允許您控制選擇多個文件。

<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 
+0

這是在4.5版嗎?我正在使用較低版本 –

+0

所有版本支持@Anand Thangappan代碼 – skhurams

+0

什麼是flBanner? – User7291

26

這裏是你如何選擇和使用文件上傳控件上傳多個文件在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; 
       } 
      } 
     } 
    } 

並在這裏你去...你的多個文件上傳控制準備好了。祝你有美好的一天。

+0

這段代碼非常感謝。然而HttpPostedFile.ContentLength是以字節表示的,而不是以千比特來表示的...... –

0

你可以試試下面的代碼:

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 
2

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 
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 
+0

這實際上起作用了,應該是被接受的答案。 – Rich

1

要添加的多個文件使用以下代碼

<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; 
} 
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); 
} 
1

在.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文件上傳標籤

,添加像類=「多「

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

如果您想要工作示例,請轉至link下載示例。

相關問題