【发布时间】:2016-07-04 14:21:44
【问题描述】:
我正在尝试下载一个仅包含一个极长的逗号分隔字符串的 CSV 模板。我的 API 端点是这样的:
public HttpResponse DownloadTemplate()
{
var attachment = "attachment; filename=template.csv";
var headers = "Header1,Header2,Header3,HeaderAndSoOnAndSoForth";
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(headers);
return HttpContext.Current.Response;
}
这是由一个简单的 AngularJS $http.post 请求调用的:
$http.post('api/Files/DownloadTemplate').success(function (data) {
// Do something with data.
});
但是,当我检查网络响应时,似乎根本没有返回任何内容。我做错了什么?
(有朋友建议把文件存到服务器里直接下载。由于种种原因,我不能这样做。)
【问题讨论】:
-
对于“原因”/为什么 - you can't trigger download via XHR,这就是
Content-Disposition", attachment是..Hth...
标签: .net angularjs asp.net-mvc