【发布时间】:2016-11-09 23:49:18
【问题描述】:
我有一些代码可以将搜索结果导出到 CSV 文件:
$("#exportButton").click(function () {
var url = "/Stuff/ExportSearchResults";
var searchInput = readInput();
$.post(url, searchInput, function (data) {
// This is where I'm clueless.
// I'm getting data back but not sure how to make the
// browser show a prompt to download the file.
console.log(data);
});
});
在服务器端(ASP.NET MVC 4)有这样的:
[HttpPost]
public FileResult ExportSearchResults(SearchInput model)
{
string csv = GenerateCsv(model);
return File(new UTF8Encoding().GetBytes(csv), "text/csv", "Export.csv");
}
所以好消息是我正在控制台中取回数据。我只是不确定如何让浏览器显示下载文件的提示。
【问题讨论】:
-
简短回答,你不应该使用 ajax 来完成这种任务,而是直接调用......
-
成功跨浏览器可能会很痛苦 - 也许FileSaver 会有所帮助
-
window.location.href = '/Stuff/ExportSearchResults';试试这样
标签: javascript c# jquery ajax