Reputation: 5709
I have a setup where I need to download multiple files (at least 2) from a database where each file is stored as a binary array through ASP. I found an example from Microsoft that almost did the trick but I seem to be unable to download multiple files. It always downloads 1 file (even if there are 2) and then calls it "FullActivity" (which is the name of a Class I've made). Looks like this:
protected void download_btn_Click(object sender, EventArgs e)
{
Int32 id = Int32.Parse(Request.QueryString.Get(0));
List<ActivityFile> files = Facade.GetActivityFiles(id);
StringBuilder msg = new StringBuilder();
if (files.Count == 0)
{
msg.Append("No files were found for this activity.");
String jsExec = Util.ModalAlert(msg.ToString(), "#info_modal", ".modal-body");
ScriptManager.RegisterStartupScript(Page, GetType(), "ModalAlertShow", jsExec, false);
}
else
{
foreach (ActivityFile file in files)
{
//TODO: Perhaps find a dynamic way of doing this
String folder = "\\\\jetfile01\\Users\\" + Util.GetUserAccount(this);
switch (file.FileType)
{
case "pdf":
Response.ContentType = "Application/pdf";
break;
case "docx":
Response.ContentType = "Application/msword";
break;
case "doc":
Response.ContentType = "Application/msword";
break;
case "xlsx":
Response.ContentType = "Application/x-msexcel";
break;
case "xls":
Response.ContentType = "Application/x-msexcel";
break;
default:
Response.ContentType = "text/plain";
break;
}
String filepath = MapPath(file.Name + "." + file.FileType);
Response.BinaryWrite(file.FileBuffer);
Response.Flush();
}
Response.End();
}
}
I am not sure what I am missing and I don't know how I would actually name the file either. My ActivityFile class does contain the full name and extension of the file but I'm unsure on how to use this information in this scenario.
Any help?
Upvotes: 1
Views: 1346
Reputation: 4517
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.
[Edit]
I just thought about this a bit more and I'm no-longer convinced it's true!
It is how I'd probably do it though.
[/Edit]
IFRAME solution HTML
<iframe id='frame1' name='frame1' />
<iframe id='frame2' name='frame2' />
The script:
document.getElementById('frame1').src = 'downloadfile1.csv';
document.getElementById('frame2').src = 'downloadfile2.csv';
Upvotes: 1