我正在尝试使用以下代码将字符串下载到 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 文件。请帮忙。谢谢,