How to Download Multiple Files using the Response Class

Coreysan 1,631 Reputation points
2024-02-24T03:50:39.54+00:00

I have a classic ASP.Net app that downloads a file using the Response class:

            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(txt_inputfile.Text));
            Response.AddHeader("Content-Length", txt_inputfile.Text.Length.ToString());
            Response.ContentType = "text/plain";
            Response.TransmitFile(txt_inputfile.Text);
            Response.Flush();
            Response.End();

Can this class be used when I want to download multiple files in one event? I use a 4.8 .Net framework.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,347 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,247 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2024-02-26T06:44:14.27+00:00

    Hi @Coreysan,

    Each file download is a response.

    Each request can only have one response (apart from push notifications of course!)

    Instead, render a page which either contains links, or makes seperate requests to download each file.

    Or, download a zip archive containing multiple files.

    You can try compressing multiple files into a single Zip Archive file using the DotNetZip library in ASP.Net.

    Demo

    protected void DownloadFiles(object sender, EventArgs e)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AlternateEncodingUsage = ZipOption.AsNecessary;
            zip.AddDirectoryByName("Files");
            foreach (GridViewRow row in GridView1.Rows)
            {
                if ((row.FindControl("chkSelect") as CheckBox).Checked)
                {
                    string filePath = (row.FindControl("lblFilePath") as Label).Text;
                    zip.AddFile(filePath, "Files");
                }
            }
            Response.Clear();
            Response.BufferOutput = false;
            string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment; filename=" + zipName); 
            zip.Save(Response.OutputStream);
            Response.End();
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2024-02-24T11:16:52.8266667+00:00

    Can this class be used when I want to download multiple files in one event?

    No. The browser can download one file at a time. For multiple files zip the files in into one zipped file and download the zipped file.