我有一个安装程序,我需要从数据库中下载多个文件(至少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