Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download multiple files using asp and C#

I'm pretty new at this, so bear with me. Here's my code. It only downloads one file even though multiple are selected.

foreach(String fileName in fileNameList)
{
    FileInfo updateFile = new FileInfo("C:/inetpub/wwwroot/w4/DanyaWebReports/Data/" + fileName);
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(updateFile.FullName) + "\"");
    Response.AddHeader("content-length", updateFile.Length.ToString());
    Response.TransmitFile(updateFile.FullName);
    Response.Flush();
}
like image 358
Bender Avatar asked Jan 08 '13 22:01

Bender


1 Answers

that is not the way to go, you can either zip all selected files server side and download only the zip file or you can try to use client side code to open multiple download windows, but in that case I think some browsers could potentially block the popups with their popup blockers.

something like, you create a page called download.aspx ( or even just an http handler ) then you call it multiple times via JavaScript:

window.open("download.aspx?id=id of file1");
window.open("download.aspx?id=id of file2");

check here for some ideas you can further elaborate: ASP.NET Download Multiple files

like image 176
Davide Piras Avatar answered Oct 04 '22 12:10

Davide Piras