gpt4 book ai didi

javascript - .net core mvc get Controller 方法返回下载文件,但不起作用?

转载 作者:行者123 更新时间:2023-11-28 03:07:08 25 4
gpt4 key购买 nike

我有List<model>我在 Javascript 中转换为 JSON,当我单击按钮时调用 Controller 方法并像这样传递参数:

$('#exceldownload').click(function(){

var json = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ReportListModel,Newtonsoft.Json.Formatting.Indented));
json = JSON.stringify(json);


window.location = "@Url.Action("ReportExcel","Report")?model="+json+"";

});

和 Controller 代码:

public FileResult ReportExcel(string model)
{
var b = JsonConvert.DeserializeObject<List<ReportListModel>>(model);
if (b.Count == 0)
{
return File(Encoding.UTF8.GetBytes("empty"), "text/plain", "empty");
}
else
{
DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(b), (typeof(DataTable)));

using (var excelPack = new ExcelPackage())
{
var ws = excelPack.Workbook.Worksheets.Add("WriteTest");
ws.Cells.LoadFromDataTable(table, true, OfficeOpenXml.Table.TableStyles.Light8);
var FileBytesArray = excelPack.GetAsByteArray();
return File(FileBytesArray, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");
}
}
}

但是当我点击按钮并得到这样的结果:

无法访问该网站,本地主机拒绝连接,ERR_CONNECTION_CLOSED

我想当我点击按钮时下载 Excel 文件。

最佳答案

它在这一行崩溃了:

window.location = "@Url.Action("ReportExcel","Report")?model="+json+"";

更改为

window.location = @Url.Action("ReportExcel","Report") + "?model="+json+"";

关于javascript - .net core mvc get Controller 方法返回下载文件,但不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60507312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com