强制文本文件下载chrome(第29版)问题

时间:2013-08-29 21:33:46

标签: c# asp.net google-chrome

我们刚刚开始在Chrome v.29浏览器上遇到文件下载问题。系统不会提示用户下载文件。

请参阅以下代码:

context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();

context.Response.AddHeader("Content-Disposition", "attachment; filename=Test.txt");
context.Response.CacheControl = "private";
context.Response.ContentType = "text/plain";
byte[] data = new byte[Encoding.UTF8.GetByteCount("Test")];
data = Encoding.UTF8.GetBytes("Test");
context.Response.AppendHeader("Content-Length", data.Length.ToString());
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.BinaryWrite(data);
context.Response.Flush();
context.Response.End();

http header / body:

HTTP/1.1 200 OK
Date: Thu, 29 Aug 2013 21:47:44 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Content-Disposition: attachment; filename=Test.txt
Content-Length: 4
Cache-Control: private
Content-Type: text/plain

Test

相同的代码适用于除chrome之外的所有浏览器!它曾经在chrome上工作,直到推出版本29 !!!

1 个答案:

答案 0 :(得分:2)

我总是使用以下内容而没有任何问题:

context.Response.ContentType = "application/octet-stream"; 

context.Response.ContentType = "application/download"; 

如果您仍遇到问题,请查看chrome extensions

使用Chrome v.29下载示例(包含Content-Type:“application / octet-stream”)

Application Octet-Stream Download

和chrome版本29.0.1547.62

enter image description here