views:

715

answers:

2

I have a simple web site with two pages. One displays a list of files, and the other streams a file when it's clicked in the list. All was fine in production for 6 months, but now I have to move the site to Windows 2008/IIS7. I have it mostly working, but the files don't open properly (in Firefox) because my content-type header is being ignored. On the production site (IIS6) the headers are (using Fiddler):

HTTP/1.1 200 OK
Date: Tue, 09 Feb 2010 16:00:51 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename="myfile__foo.pdf"
Content-Length: 236841
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1
Content-Type: application/octet-stream

but on the test IIS7 server I get:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 236841
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment; filename="myfile__foo.pdf"

Fiddler also reports a protocol violation and says "Content-length mismatch: Response Header claimed 236841 bytes, but server sent 238378 bytes."

My code looks like this:

            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + doc.DisplayFilename + "." + doc.FileExtension + "\"");
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.TransmitFile(file.FullName);
            Response.End();

I've been trying to fix this for a couple of days. I'm not that familiar with IIS7 and have struggled to find where to change various settings. I did manage to figure out about the two Managed Pipeline Modes and fixed some other issues by switching to Classic .NET AppPool (Integrated was throwing all sorts of errors when transmitting the file).

Is there some way to tell IIS7 to not overwrite my ContentType header, if that's what is happening?

A: 

You have no HttpModules modifying the Request/Response? Are the web.config files exactly the same?

Where are these extra 2K bytes coming from? What's in them?

Bryan
It turns out had nothing to do with IIS7 - it had to do with some error handling code I had added.Response.End() seems to be a mistake, even though numerous web examples contain that line. It causes a "Thread was being aborted" exception. This caused my error handler to run, which does a Server.Transfer to an error page. So the response had othe headers, the file, and the beginning of the error page's HTML at the bottom. When the error page started being written it flipped the contenttype back to HTML.Short story: Removing the Response.End() statement solved the problem.
John Price
Very interesting! Have not seen this behavior from Response.End before... I will keep it in mind.
Bryan
Apparently I experienced the same issue too and removing Response.End() solved it. It looks like it was undoing my custom Content-Type header (image/tiff) and reset it to text/html causing the browser to be confused.
Philippe Monnet
A: 

See my recent comment.

John Price