Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send the client multiple files to download

Tags:

c#

asp.net

I'm using a handler(.ashx) to serve some files. I have a folder where I store ebooks. I name them by the books PK, and each book may have a few different formats:

211.html
211.pdf
211.prc

The following test code successfully downloads one book.

context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.pdf");
context.Response.TransmitFile(context.Server.MapPath("~/Media/eBooks/212.pdf"));

How can I serve the client the three different formats? (The clients existing organization isn't in a folder)

I was trying to do something like this:

DirectoryInfo bookDir  = new DirectoryInfo(context.Server.MapPath("~/Media/eBooks"));
FileInfo[] f  = bookDir.GetFiles();

foreach (var n in f)
{
    context.Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.pdf");
    context.Response.TransmitFile(context.Server.MapPath("~/Media/eBooks/212.pdf"));
}

But it downloads one file with no file extension.

like image 576
The Muffin Man Avatar asked May 06 '11 06:05

The Muffin Man


People also ask

How do I download multiple files at once?

Hold CTRL and click on the files you want to download. Once you have selected the files you want, right click on the last file you selected and select download.

How do I download multiple files from a link?

As indicated, you can draw a rectangle around the links you want to select. This will highlight the links in yellow. From there you can either hit Enter to open the selected links in the same window, “Shift + Enter” to open in a new window, or “Alt + Enter” to download them.


1 Answers

The only way you can send multiple files in one response is to put them inside an archive package, e.g. a .zip file. That is at least something that can be done with code, using various tools (IIRC there's a zip packager inside the main .NET framework now; otherwise, SharpZipLib will do the job nicely).

like image 170
Marc Gravell Avatar answered Sep 28 '22 00:09

Marc Gravell