我有一个方法,在控制器中返回一个FileResult
下载从MVC控制器中的文件:如何使用jQuery和基因敲除
public FileResult DownloadRequestsAsCsvFile()
{
var contentType = "text/csv";
var content = CreateCsvFileOfPendingLeadRequests();
var bytes = Encoding.UTF8.GetBytes(content);
var result = new FileContentResult(bytes, contentType);
string n = string.Format("LeadRequests-{0:yyyy-MM-dd_hh-mm-ss-tt}.csv", DateTime.Now);
result.FileDownloadName = n;
return result
}
我有一个淘汰赛函数来调用控制器方法,以下载文件时,方法被调用并且方法完成,但是文件不是使用浏览器下载的。如何让浏览器下载文件?
self.getCsvFileOfRequests = function() {
$.get('../Home/DownloadRequestsAsCsvFile', function (csv) {
}).done(function() {
toastr.success("File downloaded successfully.")
}).error(function() {
toastr.error("There was a problem downloading the file.");
});
}