Asp.net多个文件上传

时间:2016-03-29 10:47:46

标签: c# asp.net file-upload asp.net-session

我有一个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 个答案:

答案 0 :(得分:1)

您可以使用JavaScript向DOM添加其他<input type="file" />元素,并通常使用MSDN中提到的Request.Files引用所有文件。从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 />");
}