How to Download Multiple Files using ASP.NET?

后端 未结 2 1376
别那么骄傲
别那么骄傲 2020-12-11 22:03

I am trying to Download multiple files using asp.net. I have a Button called DownloadFileButton and a ArrayList called FilePath(It holds all the File paths). So when i click

相关标签:
2条回答
  • 2020-12-11 22:24

    how to overcome this?

    In a word (or two), you don't.

    HTTP is a request/response system. Any response has to come as a reply to a request. Given that, you can't send multiple responses to a single request. If nothing else, there would be no client listening for those responses (because it already got the response it was waiting for).

    So essentially you have two options:

    1. Issue multiple requests, one for each "file" being downloaded. This will create multiple responses for the client to expect.
    2. Combine the files into a single file using some archiving tool (Zip libraries are pretty standard for this) in the server-side code and send that file as the response. The client would then need to un-archive it. (If the client is a user, they'd do it manually. A self-extracting executable Zip helps with that. If the client is an application, the same library can be used client-side to extract the contents of the archive and save the files.)

    One request = one response.

    0 讨论(0)
  • 2020-12-11 22:37

    You can create two or more buttons with asp.net functions to trigger after. For example:

    
        a href="javascript:myFunction1()">Call function /a>
        asp:Button ID="myButton1" runat="server" Text="Button1" OnClick="myButton1_Click" />
        asp:Button ID="myButton2" runat="server" Text="Button2" OnClick="myButton2_Click" />
        asp:Button ID="myButton3" runat="server" Text="Button2" OnClick="myButton3_Click" />
    
    

    To download multiple files you need "a new response", you can do that using $.ajax with jQuery, for example:

    
            function myFunction1()
            {
                $.ajax({
                    url: "myPage.aspx",
                    context: document.body
                }).done(function () {
                    $("#myButton1").trigger("click");
                    myFunction2().delay(500000); //delay is necessary
                });
            }
            function myFunction2() {
                $.ajax({
                    url: "myPage.aspx",
                    context: document.body
                }).done(function () {
        $("#myButton2").trigger("click");
                myFunction3().delay(1000000);
            });
        }
        function myFunction3() {
            $.ajax({
                url: "myPage.aspx",
                context: document.body
            }).done(function () {
                $("#myButton3").trigger("click");
            });
        }
    
    
    0 讨论(0)
提交回复
热议问题