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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x