How to download multiple files using asp and c#

2019-01-15 10:52发布

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

1条回答
smile是对你的礼貌
2楼-- · 2019-01-15 11:31

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

查看更多
登录 后发表回答