javascript - .net core mvc get controller 方法返回下载文件,但不起作用?
问题描述
我有List<model>
并且我在 Javascript 中转换为 JSON,当我单击按钮调用控制器方法并像这样传递参数时:
$('#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+"";
});
和控制器代码:
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");
}
}
}
但是当我点击按钮并得到这样的:
无法访问此站点,Localhost 拒绝连接,ERR_CONNECTION_CLOSED
我想当我单击按钮下载 excel 文件时。
解决方案
它在这一行崩溃:
window.location = "@Url.Action("ReportExcel","Report")?model="+json+"";
将其更改为
window.location = @Url.Action("ReportExcel","Report") + "?model="+json+"";
推荐阅读
- scala - 我想反序列化 scala 中的 json 对象
- python - 有没有办法从 2D (x,y) 数据集中绘制同心圆颜色图,其中 x 是 python 中的圆的半径
- php - Symfony 5 在某些请求中没有检测到正确的语言环境
- excel-formula - 如何在 Excel 的行项目中添加“上次更新日期”?
- jmeter - Jmeter 读取 CSV 文件返回
写在上面之后 - dns - Name.com 裸域到 www
- php - Tenancy - 无法声明类 CreateUsersTable,因为该名称已在使用中
- reactjs - Next.js / React 单页应用程序需要每个项目的 url
- javascript - 如何从函数返回与传递给函数的数量一样多的元素(html标签)?反应
- python - Python打印与输入重叠