【发布时间】:2015-06-20 05:25:20
【问题描述】:
这是下载文件的逻辑。
它会自动下载文件,无论是 .jpg、.pdf、.zip 还是其他文件,如果它是图像文件(jpg、png 等),我希望它只在新选项卡中打开,然后是所有文件else 提示下载。
在解决这个问题时,我觉得我错过了一些东西。 :/
public void DownloadArchivedFiles(ArchiveType type, Object id, String fileName)
{
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + fileName);
String path = Server.MapPath(String.Format("~/{0}Files/{1}", type, id));
var fmFiles = new string[0];
var files=new string[0];
if(type==ArchiveType.Issue)
{
fmFiles = _files.GetIssueFiles(new Guid(id.ToString())).Select(x => Server.MapPath("~" + x.FilePath)).ToArray();
}else if(type==ArchiveType.Task)
{
fmFiles = _files.GetTaskFiles(int.Parse(id.ToString())).Select(x => Server.MapPath("~" + x.FilePath)).ToArray();
}
if (!System.IO.Directory.Exists(path) && !fmFiles.Any()) return;
//String[] files = System.IO.Directory.GetFiles(Server.MapPath(String.Format("~/{0}Files/{1}", type, id)));
try
{
files = System.IO.Directory.GetFiles(Server.MapPath(String.Format("~/{0}Files/{1}", type, id)));
}
catch (Exception)
{
;
}
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(files, "/");
if(fmFiles.Any())
zip.AddFiles(fmFiles,"/");
zip.Save(Response.OutputStream);
}
HttpContext.Response.End();
}
【问题讨论】: