我在ASP.NET页面中动态生成Zip文件,然后将流发送到Response。

在Firefox中,我可以下载名为 Images.zip 的文件。它工作正常。在Internet Explorer 7中,它尝试下载一个名为 ZipExport.aspx 的文件,或者如果它在Generic Handler中, 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因忽略我的内容处置标题而出错?

编辑:我删除了这些行并且工作正常:

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();
}
有帮助吗?

解决方案

Response.End 替换为 HttpContext.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来查看可能出现的其他问题。

其他提示

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

取代Response.ClearHeaders(),执行完整的 Response.Clear(),然后执行 Response.End()

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

可能有助于未来搜索者的一点是,该问题仅在HTTPS网站上出现。代码在我的HTTP本地服务器上正常运行。

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

我从未使用过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();

我刚遇到同样的问题,并设法修复

Response.Clear();                         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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top