2016-03-29 58 views
0

我有一個ASP.net網絡應用程序,它保存輸入會話中的所有詳細信息,並在最後發送一個電子郵件,其中包含上傳文件的附件(所有這些當前工作正常)的所有詳細信息。Asp.net多文件上傳

我遇到的問題是文件上傳頁面需要更改。我想現在能夠上傳多個文檔,但是分開,所以基本上用戶找到要上傳的文件,然後單擊「添加」按鈕。然後應該清除上傳文件字段,並且應該爲每個上傳的文件使用「刪除/刪除」按鈕來填充上傳的文件。

應顯示在此表中的細節是:

  • 文件名
  • 選擇選項形式新的下拉列表
  • 刪除按鈕

正如我說我有一個文件上傳使用後面的代碼工作,但我不知道如何實現我以後的內容,以便所有上傳的文檔都通過我的電子郵件發送。

當前HTML

<div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-5"> 
      <div class="fileinput fileinput-new input-group" data-provides="fileinput"> 
       <div class="form-control" data-trigger="fileinput" style="background-color: #ededed"> 
        <span class="fileinput-filename"></span> 
       </div> 
       <span class="input-group-addon btn btn-default btn-file"> 
        <span class="fileinput-new"> 
         <span class="glyphicon glyphicon-folder-open" title="Click to select a file."></span> 
        </span> 
        <span class="fileinput-exists"> 
         <span class="glyphicon glyphicon-folder-open" title="Click to change the selected file."></span> 
        </span> 
        <input type="file" name="..." id="fuAttachment" runat="server" /> 
       </span> 
      </div> 
    </div> 
    <div class="col-sm-3"> 
      <asp:DropDownList ID="Step03WebTypeDD" runat="server" class="form-control"> 
       <asp:ListItem Value="- - Please select - -">- - Please select - -</asp:ListItem> 
       <asp:ListItem Value="Requirements">Requirements</asp:ListItem> 
       <asp:ListItem Value="Functional specification">Functional specification</asp:ListItem> 
       <asp:ListItem Value="Page Content">Page Content</asp:ListItem> 
       <asp:ListItem Value="Page Designs">Page Designs</asp:ListItem> 
       <asp:ListItem Value="Page Designs">Other</asp:ListItem> 
      </asp:DropDownList> 
    </div> 
    <div class="col-sm-1">          
      <asp:LinkButton ID="UploadAddButton" runat="server" OnClick="Step05UploadAddButton_Click" CssClass="btn btn-success pull-right" ToolTip="Click to upload the file."> 
       <span class="glyphicon glyphicon-plus"></span> Add 
      </asp:LinkButton> 
    </div> 
</div> 

目前代碼背後

protected void Step05UploadAddButton_Click(object sender, EventArgs e) 
{ 
    var file = fuAttachment.PostedFile; 
    if (file != null && fuAttachment.PostedFile.FileName != "") 
    { 
      var content = new byte[file.ContentLength]; 
      file.InputStream.Read(content, 0, content.Length); 
      Session["FileContent"] = content; 
      Session["FileContentType"] = file.ContentType; 
      Session["File"] = fuAttachment.PostedFile.FileName; 
      Session["AttachmentProvided"] = "Yes"; 
    } 
    else 
    { 
      Session["FileContent"] = ""; 
      Session["FileContentType"] = ""; 
      Session["File"] = ""; 
      Session["AttachmentProvided"] = "No"; 
    } 
} 

我後面的代碼不會捕獲選擇的選項,但需要,所以我可以在我的確認頁上顯示它( HTML如下所示)

<div class="form-group"> 
     <asp:Label ID="Label6" class="col-xs-6 col-sm-3 control-label" runat="server" Text="Attachment provided:"></asp:Label> 
     <div class="col-xs-6 col-sm-9 form-control-static"> 
      <% if (Session["AttachmentProvided"] == "Yes") 
      {%> 
       Yes (<%=Session["File"] %>) 
      <%} 
      else 
      { %> 
       No 
      <%} %> 
     </div> 
    </div> 

回答

1

您可以使用JavaScript將其他<input type="file" />元素添加到DOM中,並且一般指的是使用Request.Files的所有文件,如MSDN中所述。取自MSDN的樣本:

Files = Request.Files; // Load File collection into HttpFileCollection variable. 
arr1 = Files.AllKeys; // This will get names of all files into a string array. 
for (loop1 = 0; loop1 < arr1.Length; loop1++) 
{ 
    Response.Write("File: " + Server.HtmlEncode(arr1[loop1]) + "<br />"); 
    Response.Write(" size = " + Files[loop1].ContentLength + "<br />"); 
    Response.Write(" content type = " + Files[loop1].ContentType + "<br />"); 
}