asp.net-mvc Web API 返回 csv 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29948809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web API return csv file
提问by ShaneKm
I need to get a csv file from web api controller. I can not get "Save As" dialog to show up. Only text output shows up on the page. I tried both, calling Export from jquery and also plain old html
我需要从 web api 控制器获取一个 csv 文件。我无法显示“另存为”对话框。页面上只显示文本输出。我都试过了,从 jquery 调用导出,也尝试了普通的旧 html
Controller:
控制器:
[System.Web.Http.HttpGet]
public HttpResponseMessage Export()
{
StringBuilder sb = new StringBuilder();
IEnumerable<CustomerDiscount> list = this.subscriberRepository.GetSubscribers();
foreach (CustomerDiscount item in list)
{
sb.AppendFormat(
"{0};{1};{2};",
item.CustomerName,
item.CustomerNumber,
Environment.NewLine);
}
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(sb.ToString());
writer.Flush();
stream.Position = 0;
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("text/csv");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
return result;
}
EDIT: added this line:
编辑:添加了这一行:
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
still doesn't work
还是不行
I call it like this:
我这样称呼它:
<a id="export" href="/Relay/Billing/Export" class="btn btn-primary">Export</a>
and also tried it like this:
也试过这样:
$("#export").click(function () {
$.post("/Relay/Billing/Export", { type: $("#discountType").val() })
.done(function (data) {
});
});
Still no Save As box
仍然没有另存为框
回答by lbrahim
I do not know whether this is proper protocol but this is how I did it before. So you have a web page from which you will invoke an API that will response with the file and it should be handled by browser's save as dialog.
我不知道这是否是正确的协议,但我以前是这样做的。因此,您有一个网页,您将从中调用一个 API,该 API 将响应文件,并且应该由浏览器的另存为对话框处理。
The HTML:
HTML:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function save() {
window.open('http://localhost:45719/api/home?id=12', '_blank', '');
}
</script>
</head>
<body>
<a class="btn btn-primary" onclick="save()">Export</a>
</body>
</html>
The action:
那个行动:
public HttpResponseMessage Get(int id)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write("Hello, World!");
writer.Flush();
stream.Position = 0;
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
return result;
}
This is working for me in both Chrome and Firefox latest.
这在最新的 Chrome 和 Firefox 中对我有用。

