如何使用C#在ASP.NET中选择多个文件

时间:2015-05-28 08:19:51

标签: c# asp.net

我想编写一个应用程序,允许您使用C#通过ASP.NET中的FileUpload选择多个文件,我该怎么做?

3 个答案:

答案 0 :(得分:2)

您可以添加allowmultiple属性

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

查看this example

答案 1 :(得分:2)

.NET 4.5&amp;上面:

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

.NET 4及以下版本:

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

html:

<div>
    <asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
    <asp:Button runat="server" ID="uploadedFile" Text="Upload" OnClick="uploadFile_Click" />
    <asp:Label ID="listofuploadedfiles" runat="server" />
</div>

按钮点击代码:

protected void uploadFile_Click(object sender, EventArgs e)
{
   if (UploadImages.HasFiles)
   {
       foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
       {
           uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),
           uploadedFile.FileName)); listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
       }
   }
} 

更多reference

答案 2 :(得分:1)

您可以编写提交控制器方法,如:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Submit(IEnumerable<HttpPostedFileBase> files)
{
    //your file code here 
}

然后只是为您喜欢的上传提供相应的视图,并回发文件。例如Jquery文件上传小部件:https://blueimp.github.io/jQuery-File-Upload/

ASP.NET MVC中的Kendo Ui示例:

 <form method="post" action='@Url.RouteUrl("Default", new {Action =     "Submit"})' style="width:45%">
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="float-left">
    //widget here 
        <p>
            <input type="submit" value="Submit" class="k-button" />
        </p>
    </div>
  </form>