1

我在 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();
}
4

6 回答 6

3

您应该为此创建一个ASHX 处理程序。您是否尝试过使用“应用程序/zip”的内容类型?

于 2009-07-30T20:06:50.260 回答
3

替换Response.EndHttpContext.Current.ApplicationInstance.CompleteRequest

试试这个精​​简版:

Response.Clear();
Response.BufferOutput = false;

Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
using(ZipFile zip = new ZipFile())
{
  zip.AddFile(Server.MapPath("sample1.png"));
  zip.Save(Response.OutputStream);
}
HttpContext.Current.ApplicationInstance.CompleteRequest();

如果失败,请使用 Microsoft Fiddler 查看其他可能出现的问题。

于 2009-07-30T20:16:34.203 回答
2

代替 Response.ClearHeaders(),做一个完整的Response.Clear(),然后,做一个Response.End()

于 2009-07-30T20:07:09.917 回答
1

我刚遇到同样的问题(并修复)谢谢。

可能对未来的搜索者有所帮助的一点是,这个问题只出现在 HTTPS 网站上。该代码在我的 HTTP 本地服务器上运行良好。

我猜想使用 HTTPS 无论如何它都不会被缓存,因此可以包含在“if(Request.IsSecureConnection)”条件中。

于 2009-09-02T19:46:44.943 回答
0

我从来没有使用过 ZipFile 类,当我发送文件时我使用 Response.BinaryWrite()

//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("content-disposition", "attachment;filename=\"" + currentDocument.Name + "\"");



//currentDocument.Document is the byte[] of the file
context.Response.BinaryWrite(currentDocument.Document);

context.Response.End();
于 2009-07-30T20:16:50.927 回答
0

我刚刚遇到同样的问题并设法解决

响应。清除();Response.BufferOutput = false;

                    Response.ContentType = "application/zip";
                    //Response.AddHeader("content-disposition", "inline; filename=\"" + ArchiveName + "\"");
                    Response.AddHeader("content-disposition", "attachment; filename=\"" + ArchiveName + "\"");
                    zipFile.Save(Response.OutputStream);
                   // Response.Close();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    Response.Clear();
                    Response.End();
于 2017-05-08T12:12:06.093 回答