Firefox is for some reason not properly handling content sent through my .NET HTTP handler; it appears to be not honoring the content type header. Rather, it sees the content as being HTML. The URL mapped to the request even has a .csv extension. Internet Explorer and Chrome are doing the right thing. The problem is happening for both a "text/css" and an "application/pdf" handler.
Here's a fragment of my HTTP handler's ProcessRequest method:
public void ProcessRequest(HttpContext context)
{
// ...
// Set the output headers
context.Response.ClearHeaders();
context.Response.ContentType = "text/csv";
context.Response.AddHeader(
"Content-Disposition", "attachment; filename=foo.csv");
// Code that writes to the output stream
// ...
context.Response.End();
}
What's missing from my response that would enable Firefox to recognize the content type as expected?
Edit 1:
When using the Firefox Live HTTP Headers extension, I saw, I'm getting back the following headers. It looks like my ContentType header is getting lost.
HTTP/1.x 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Thu, 31 Dec 2009 02:34:09 GMT
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment;filename="foo.csv"
Cache-Control: private
Content-Type: text/html
Content-Length: 66682
Connection: Close
Edit 2:
Found the issue. In my handler, I was using context.Server.Execute
to generate HTML from an ASPX template, then processing that HTML. In other words, I was not using context. Server.Execute
to direc开发者_开发问答tly output to the response. Despite this, running that method modify's the current context's response headers. So this was undoing the headers I had set. Moving the code that modifies the headers to after context.Server.Execute
solved the problem.
The reason this only affected Firefox is because the other browsers use the file extension rather than the content type. Firefox does the right thing.
That does seem odd. I would install the Live HTTP Headers add-on for Firefox just to confirm that Firefox is indeed seeing those two headers as you'd expect.
RFC 2616 also seems to suggest placing quotes round the filename, so you could try that out as well.
精彩评论