IE11在Response.BinaryWrite中写入HTML而不是byte()内容

时间:2014-08-13 07:47:42

标签: asp.net internet-explorer-11

我有一个ASP.NET(Framework 2.0)网站,它生成一个CSV文件,并提示用户保存或打开此文件(IE本机弹出窗口)。最近一些用户将他们的IE升级到版本11,他们得到的文件包含HTML作为内容,而不是实际的CSV样式行。 这适用于IE9及以下版本。

Response.BinaryWrite的代码:

Response.Buffer = False
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment;filename=FileExport.csv")
Response.BinaryWrite(Session("nExport"))
Session("nExport") = Nothing
Response.Flush()
Response.Close()
Response.End()
' Session("nExport") contains Byte()

预期的文件内容:

NR;FirstName;LastName
1;Bilbo;Baggins

如果我选择保存或打开文件内容:

"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"

"<html xmlns=""http://www.w3.org/1999/xhtml"" >"
<head><title>
"   Export"
</title></head>
<body>
"    <form name=""form1"" method=""post"" action=""nExport.aspx?ID=RGVwbjsu3GFyamkyMDEz"" id=""form1"">"
<div>
"<input type=""hidden"" name=""__VIEWSTATE"" id=""__VIEWSTATE"" value=""/wEPDwUJNzgzNDMwNTMzZGSmoL1exQ/68hIbp0DWxDDjlt6wAA=="" />"
    </div>

    <div>

    </div>
    </form>
</body>
</html>

编辑:用户提供的其他信息是另一个(类似的)CSV文件下载工作正常。我将不得不调查一下,两个下载的代码都是一样的。

编辑2: 它似乎在UAT服务器上工作,解决方案是删除Response.End和Response.Close并使用HttpApplication.CompleteRequest,根据这个问题ie-10-file-download-issues。将不得不调查更多并尝试生产服务器。

Dim httpApp As New HttpApplication
httpApp.CompleteRequest()
'Response.Close()
'Response.End()

1 个答案:

答案 0 :(得分:3)

我也可以在生产服务器上确认工作解决方案。正如stunthis answer的评论建议我在CompleteRequest之后离开Response.Close和Response.End。

这是新的Response方法:

Response.Buffer = False
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment;filename=FileExport.csv")
Response.BinaryWrite(Session("nExport"))
Session("nExport") = Nothing
Response.Flush()

HttpContext.Current.ApplicationInstance.CompleteRequest()

Response.Close()
Response.End()

编辑只是修复,使用HttpContext.Current而不是新的HttpApplication。