简体   繁体   English

如何从 GridView 的 ASP.NET Web Z645024253191293266Z Z6450242531912981C3683CAE88DZ 内部同时上传多个文件?

[英]How to upload multiple files simultaneously from inside GridView of ASP.NET Web Forms using JavaScript/jQuery?

I have a GridView and some columns in it.我有一个GridView和其中的一些列。 I have created a template column inside the GridView, and in it a file upload control is placed.我在 GridView 内部创建了一个模板列,并在其中放置了一个文件上传控件

<asp:GridView ID="GridView1" runat="server">
  <Columns>
    <ItemTemplate>
      <asp:FileUpload id="FileUploadControl" runat="server" />
    <ItemTemplate>
  </Columns>
</asp:GridView>

The GridView is binded with data so that it has multiple rows. GridView 与数据绑定,使其具有多行。 These is a button given below the GridView and on the the button click I want to upload all the files of the file upload control (at the same time) to the server.这是GridView下方给出的按钮,在按钮上单击我想将文件上传控件的所有文件(同时)上传到服务器。

I have seen a similar tutorial on Multiple File Upload which does a similar thing, only difference is that I have not one but multiple files to upload.我看过一个关于多文件上传的类似教程,它做了类似的事情,唯一的区别是我没有一个而是多个文件要上传。

I want to use JavaScript or jQuery to do it.我想用 JavaScript 或 jQuery 来做。

Please help me in this problem.请帮我解决这个问题。

Thanks谢谢

You have to allow uploading multiple files in your file upload control like this:您必须允许在文件上传控件中上传多个文件,如下所示:

<asp:FileUpload id="FileUploadControl" AllowMultiple="true" runat="server" /> 

Or:或者:

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

Use HttpFileCollection class to retrieve all the files to upload:使用HttpFileCollection class 检索所有要上传的文件:

    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("MyFiles") + "\\" +
                  Path.GetFileName(hpf.FileName));                      
            }              
        }   
    }
    catch (Exception ex)
    {
        // Handle your exception here
    }

For details, you can take a look at here详情可以看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM