我有一個安裝程序,我需要從數據庫中下載多個文件(至少2個),其中每個文件通過ASP存儲爲二進制數組。我發現了一個微軟的例子,幾乎可以做到這一點,但我似乎無法下載多個文件。它總是下載1個文件(即使有2個文件),然後將其稱爲「FullActivity」(這是我創建的類的名稱)。看起來像這樣:通過ASP下載多個文件
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();
}
}
我不知道我失蹤了什麼,我不知道如何實際命名文件。我的ActivityFile類包含文件的全名和擴展名,但我不確定如何在這種情況下使用這些信息。
任何幫助?
一個響應對象只能存儲一個文件的字節。使用.ZIP庫(如https://dotnetzip.codeplex.com/)將所有文件壓縮成一個.zip文件。 – adaam
n.b.我以前的陳述可能不一定是真實的,它可能確實可以實現,但爲了簡單起見,只需創建一個zip文件! – adaam