我正在尝试使用以下代码将字符串下载到CSV文件中:
string attachment = "attachment; filename=Test.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write("my,csv,file,contents");
HttpContext.Current.Response.End();
我的Fiddler捕获给了我以下内容:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 18 Sep 2013 22:06:19 GMT
X-AspNet-Version: 4.0.30319
content-disposition: attachment; filename=Test.csv
Pragma: public
Cache-Control: private
Content-Type: text/csv; charset=utf-8
Content-Length: 20
Connection: Close
my,csv,file,contents
但是,浏览器没有下载任何CSV文件。 请帮忙。 谢谢,
答案 0 :(得分:0)
标题应写为Content-Disposition
- 注意名称中的大写字母。有关详情,请在http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
19.5.1 Content-Disposition
例如:
Content-Disposition: attachment; filename="fname.ext"
还要注意文件名周围的双引号。
答案 1 :(得分:0)