4

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 the Download Button only 1 file is downloaded(the first file in the FilePath List). Because Response.End() causes the script to stop processing. when i comment out the Response.End() then i get an exception at Response.ClearHeaders().

how to overcome this?

My Code:

protected void DownloadFileButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < FilePath.Count; i++)
    {
    string path = FilePath[i];

            FileInfo file = new FileInfo(path);

            if(file.Exists)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.Flush();
                Response.TransmitFile(file.FullName);
                Response.End();
            }
        }
    }
}
  • 2
    I could be wrong but as far as I know you can't send multiple files to the client as a response. You may wish to compress the multiple files though. – DandrĂ© Mar 14 '14 at 19:10

2 Answers2

13

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.

David
  • 208,112
  • 36
  • 198
  • 279
1

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");
        });
    }