.NET Framework - Response.ContentType = "application/vnd.ms-excel" IGNORED!

Asked By Trapulo on 17-Mar-10 01:15 PM
I have an application that worked well on IIS6. I moved it to IIS7, and
response.contenttype is now ignored.

my code ends with:

Response.ContentType = "application/vnd.ms-excel"

Response.Flush()
Response.End()

But the browser receives "application/octet-stream" as mime type! IE shows
the XLS file as html/text, and FFOX propts to save.

Why? :((

thanks




Mike Lovell replied to Trapulo on 17-Mar-10 11:53 PM
Could it be related to this bug report?

https://connect.microsoft.com/VisualStudio/feedback/details/480689/response-flush-clears-content-type-header?wa=wsignin1.0

I notice you are using Flush.  They suggest that if you set the Content
Length it will not happen.

--
Mike
GoTinker, C# Blog
http://www.gotinker.com
Trapulo replied to Mike Lovell on 24-Mar-10 06:37 AM
Maybe can be, but it seems impossible that I may wait for .NET 4.0 to  have
this simple and old feature running.. :((
Alexey Smirnov replied to Trapulo on 24-Mar-10 06:44 AM
have
shows
t

Do you have Response.Clear before that code? Maybe it helps
Trapulo replied to Alexey Smirnov on 24-Mar-10 08:24 AM
I added now, but still the same :(
I forgot to say that either .Flush and .End was added to try to solve this
issue: on the IIS6 I did not use them and all worked.

thanks
Trapulo replied to Trapulo on 30-Mar-10 09:41 AM
Where is MS "two business days" support?!
v-zhiqn replied to Trapulo on 31-Mar-10 07:52 AM
Hi Trapulo,

Based on my understanding, you are facing the issue that IIS7 seems to
ignore the ContentType property of a Response. However, based on my test,
it works well with both ContentType property value of

As you did not provide the complete code of this part in your page, it is
hard to say where the problem is. So I would like to share the code of my
test demo here. You may have a try with this code to see if this could help
to solve the case.

System.IO.FileInfo fi = new
System.IO.FileInfo(Server.MapPath("TestExcel.xls"));
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/octet-stream";
Response.WriteFile(fi.FullName);
Response.End();

Best Regards,
Zhi-Qiang Ni
Microsoft Online Support
v-zhiqn replied to Trapulo on 07-Apr-10 04:09 AM
Hi Trapulo,

This is Zhi-Qiang Ni  from MSDN Managed Newsgroup support team, since I
have not seen your reply after I last posted my reply, I am writing to check
the status of this post. Please feel free to let me know if there is
anything else I can help. Thanks.


Based on my understanding, you are facing the issue that IIS7 seems to
ignore the ContentType property of a Response. However, based on my test,
it works well with both ContentType property value of

As you did not provide the complete code of this part in your page, it is
hard to say where the problem is. So I would like to share the code of my
test demo here. You may have a try with this code to see if this could help
to solve the case.

System.IO.FileInfo fi = new
System.IO.FileInfo(Server.MapPath("TestExcel.xls"));
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/octet-stream";
Response.WriteFile(fi.FullName);
Response.End();

--
Regards,

Zhi-Qiang Ni

Microsoft Online Support