Iztoksson
Iztoksson

Reputation: 990

IE11 writes HTML in Response.BinaryWrite instead of byte() content

I have an ASP.NET (Framework 2.0) website which generates a CSV file and propmpts the user to Save or Open this file (IE native pop-up window). Recently some users upgraded their IE to version 11 and they get the file with HTML as content and not the actual CSV-style rows. This is working in IE9 and below.

Code for 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()

Expected file content:

NR;FirstName;LastName
1;Bilbo;Baggins

File content if I select Save or Open:

"<!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>

Edit: additional information from the user is that another (similar) CSV file download is working correctly. I will have to investigate some more, code for both downloads is the same.

Edit 2: It seems to be working on UAT server, the solution was removing Response.End and Response.Close and use HttpApplication.CompleteRequest, as per this question ie-10-file-download-issues. Will have to investigate more and try on production servers.

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

Upvotes: 2

Views: 2535

Answers (1)

Iztoksson
Iztoksson

Reputation: 990

I can confirm a working solution on the production server also. As the comment from stun in this answer suggests I left Response.Close and Response.End after CompleteRequest.

This is the new Response method:

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

Edit: just a fix, using HttpContext.Current not making a new HttpApplication.

Upvotes: 3

Related Questions