[英]Download file from DB with ajax call MVC
I'm trying to implement file download functionality thru ajax call in MVC.我正在尝试通过 MVC 中的 ajax 调用来实现文件下载功能。 After calling of controller method i always have a "parseerror", can somebody explain me why?
在调用 controller 方法后,我总是有一个“parseerror”,有人能解释一下为什么吗?
my ajax:我的 ajax:
tab.on("click", ".FileDownload", function (e) {
//$('#uploadStatus').html("ok");
var tr = $(this).closest("tr");
var id = tr.data("id");
$.ajax({
type: "POST",
url: "/File/FileDownload",
//contentType: false,
//processData: false,
//dataType: "json",
data: { fileId: id },
success: function (data) {
$('#uploadStatus').html("ok");
},
error: function (err) {
alert(err.statusText);
}
});
});
and controller:和 controller:
[HttpPost]
public FileResult FileDownload(int? fileId)
{
FileDBEntities db = new FileDBEntities();
tblFile file = db.tblFiles.ToList().Find(p => p.id == fileId.Value);
return File(file.Data, file.ContentType, file.Name);
}
with simple download link in razor it works, but not with ajax.通过 razor 中的简单下载链接,它可以工作,但不适用于 ajax。 What am I doing wrong here?
我在这里做错了什么?
Why not simple use为什么不简单使用
tab.on("click", ".FileDownload", function (e) {
//$('#uploadStatus').html("ok");
var tr = $(this).closest("tr");
var id = tr.data("id");
window.location = window.location.origin + '/File/FileDownload?fileId=' + id;
});
[HttpGet]
public FileResult FileDownload(int? fileId)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.