jquery - 从 Ajax 调用控制器操作时无法下载生成的 PDF
问题描述
我从控制器返回 FilestreamResult 我尝试了 jquery 和 ajax 两种方式,但无法成功从控制器下载 pdf/xlsx 文件。
控制器:
var filestream = new FileStream(pdfoutputpath + ".pdf", FileMode.Open);
return new FileStreamResult(filestream, "application/pdf");
使用jquery查看代码:
function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
var url = "/WorkInstructions/WorkinstructionDownload";
$.get(url, { id: id, fileformat: 2 }, function () {
});
}
使用阿贾克斯:
function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
$.ajax({
url: "/WorkInstructions/WorkinstructionDownload",
cache: false,
type: "GET",
data: { id: id, fileformat: 2 },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {
}
});
解决方案
尝试将其放入您success
的 ajax 调用函数中:
success: function(){
window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
}
虽然我不认为 ajax 在这里提供任何价值,除非我遗漏了什么。您可以轻松地获取success
我在上面发布的那个函数的内容,并将其完全放在 ajax 调用之外(并删除 ajax 调用)。或者您可以简单地执行以下操作并完全删除脚本:
<a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>
推荐阅读
- r - 开发自己的包:UseMethod 不起作用
- python - PermissionError:[Errno 13] 权限被拒绝-Spotipy
- javascript - 为什么可以直接引用document.body而其他元素不能?
- python - 如何从序列中只得到 5mers
- javascript - 想要在 Mobile Responsives 上用 5 个不同的图像制作一个可滚动和可点击的 div
- python - 使用 WEASEL 进行时间序列分类
- excel - 使用 Excel VBA 消息框
- python - 用户输入字符串的大小写不敏感比较
- gitahead - 如何解决“无效证书”
- postgresql - 使用触发器 postgresql 更新列值/从插入查询中获取值