我通过以下代码发送 JSON 对象..控制器返回 CSV 格式的值,应提示打开或保存为 CSV 文件。我无法理解成功应该编写什么代码:函数(结果)
导出链接
Html.ActionLink("Export", "", "", null, new { @onclick = "return exportData();"})
function exportData() {
var searchViewModel = getExLogSearchModelData();
var path = getAbsolutePath("ExclusionLog", "ExportData");
$.ajax({
url: path,
cache: false,
contentType: 'application/json; charset=utf-8',
dataType: 'html',
type: 'POST',
data: JSON.stringify(searchViewModel),
success: function (result) {
},
error: function (error) {
$("#voidcontainer").html("Error");
}
});
}
控制器动作结果
public ActionResult ExportData(ExclusionLogSearchViewModel postSearchViewModel)
{
try
{
IEnumerable<ExclusionLogViewModel> exclusionLogData = null;
exclusionLogcData = this._workerService.GetExclusionLogData(postSearchViewModel);
return CSVFile(exclusionLogMembershipSyncData.GetStream<ExclusionLogViewModel>(), "ExclusionLogTables.Csv");
}
catch (Exception ex)
{
throw ex;
}
return null;
}
你能建议如何做到这一点吗?
我遇到了同样的问题,但没有找到使用下载文件的方法$.ajax
但我发现了一个优秀的 JavaScript 库,它提供了类似的行为:
https://github.com/johnculviner/jquery.file下载 https://github.com/johnculviner/jquery.fileDownload
你只需要调用这样的东西:
$.fileDownload(path, {
httpMethod: 'POST',
data: JSON.stringify(searchViewModel),
success: function (result) {
},
error: function (error) {
$("#voidcontainer").html("Error");
}
});
并记住在控制器中创建 cookie。在src/Common
有合适的动作过滤器可以为您做到这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)