我在 ASP.NET 页面中动态生成一个 Zip 文件,然后将流发送到响应。
在 Firefox 中,我可以下载名为Images.zip
. 它工作正常。在 Internet Explorer 7 中,它会尝试下载一个名为的文件,ZipExport.aspx
或者如果它位于通用处理程序中,ZipExport.ashx
它会说在服务器上找不到它并失败。
这是我的代码:
Response.BufferOutput = true;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(System.TimeSpan.Zero);
ZipFile zip = new ZipFile();
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);
我不想为某个文件创建一个 HTTPHandler 并将其注册到 IIS。
我是否缺少一些简单的东西,或者 Internet Explorer 是否因忽略我的 content-disposition 标头而出错?
编辑:我删除了这些行并且工作正常:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
编辑:如果有人感兴趣,这是工作代码:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.BufferOutput = false;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-disposition",
"attachment; filename=ChartImages.zip");
context.Response.Cache.SetNoServerCaching();
context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);
using(ZipFile zip = new ZipFile())
{
zip.AddFile(context.Server.MapPath("sample1.png"));
zip.Save(context.Response.OutputStream);
}
context.ApplicationInstance.CompleteRequest();
}