在ASP.NET中传输文件在Firefox中工作,但在Internet Explorer中不工作

在ASP.NET中传输文件在Firefox中工作,但在Internet Explorer中不工作,asp.net,internet-explorer,streaming,response,Asp.net,Internet Explorer,Streaming,Response,我在ASP.NET页面中动态生成一个Zip文件,然后将流发送到响应 在Firefox中,我可以下载名为Images.zip的文件。它工作正常。在InternetExplorer7中,它试图下载一个名为ZipExport.aspx的文件,或者如果它位于一个通用处理程序中,ZipExport.ashx,它说在服务器上找不到该文件,因此失败 这是我的密码: Response.BufferOutput = true; Response.ClearHeaders(); Response.ContentTy

我在ASP.NET页面中动态生成一个Zip文件,然后将流发送到响应

在Firefox中,我可以下载名为
Images.zip的文件。它工作正常。在InternetExplorer7中,它试图下载一个名为
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因为忽略了我的内容处理头而有错

Edit:我删除了这些行,一切正常:

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.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();
}

您应该为此创建一个。您尝试过使用“application/zip”内容类型吗?

您应该为此创建一个。您尝试过使用“application/zip”内容类型吗?

而不是Response.ClearHeaders(),做一个完整的响应。Clear(),然后做一个响应。End()

而不是Response.ClearHeaders(),做一个完整的响应。Clear(),然后做一个响应。End()

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查看可能出现的其他问题。

HttpContext.Current.ApplicationInstance.CompleteRequest替换
Response.End

请尝试此精简版本:

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查看可能出现的其他问题。

我从未使用ZipFile类,也就是说,当我发送文件时,我使用Response.BinaryWrite()


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


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

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


我想使用HTTPS时,它不会被缓存,因此可以被封装在“if(Request.issecurenconnection)”条件中。

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

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


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

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

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();

我只是遇到了同样的问题并设法解决了

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();

是的-不做回应。明确可能是原因。好主意,但我不认为这真的能解决核心问题。是的-不做回应。明确可能是原因。好主意,但我认为这并不是核心问题。作为次要问题,您可能希望将BufferOutput设置为false。fiddler是否向您提供有关该问题的任何信息?作为次要问题,您可能希望将BufferOutput设置为false。fiddler是否向您提供有关该问题的任何信息?谢谢,是的,我考虑过了。Save是一种方便的方法,API会为此宣传一些东西。当然,它在Firefox中工作得很好。谢谢,是的,我考虑过了。Save是一种方便的方法,API会为此宣传一些东西。当然,它在Firefox中工作得很好。我选择这个作为答案,因为它确实解决了这个问题。经过更多测试后,问题行似乎是Response.Cache.SetCacheability(HttpCacheability.NoCache)@切特-那很好。有时回到一个更基本的版本有助于识别问题。我选择这个作为答案,因为它确实解决了问题。经过更多测试后,问题行似乎是Response.Cache.SetCacheability(HttpCacheability.NoCache)@切特-那很好。有时,返回到更基本的版本有助于识别问题。