[英]Downloading MVC FileResult doesn't work in IE or Edge browsers
I am trying to download an excel file generated from data entered through a webpage in an MVC application. 我正在尝试下载通过MVC应用程序中通过网页输入的数据生成的excel文件。
This ajax call is executed when a button is pressed, and calls two methods in my controller. 当按下按钮时,将执行此ajax调用,并在我的控制器中调用两个方法。 One to generate the excel file and another to download the file: 一种生成excel文件,另一种下载文件:
$.ajax({
type: 'POST',
data: myDataObject,
url: 'MyController/GenerateExcel/',
success: function(data) {
if (data.id != "") {
$http.get('MyController/DownloadExcel?id=' + encodeURIComponent(data.id) + '&name=' + encodeURIComponent(data.name));
return true;
}
}
});
Here is my POST method that generates the excel file and saves it to TempData: 这是生成excel文件并将其保存到TempData的我的POST方法:
[HttpPost]
public JsonResult GenerateExcel(Object model)
{
var fileName = "myexcel.xlsx";
var fileID = Guid.NewGuid().ToString();
var generatedReport = GenerateCustomExcel(model);
using (MemoryStream memoryStream = new MemoryStream())
{
generatedReport.SaveAs(memoryStream);
generatedReport.Dispose();
memoryStream.Position = 0;
TempData[fileID] = memoryStream.ToArray();
}
return Json(new { id = fileID, name = fileName });
}
Here is my GET method that downloads the saved excel from TempData: 这是我的GET方法,可从TempData下载保存的Excel:
[HttpGet]
public FileResult DownloadExcel(string id, string name)
{
if (TempData[id] != null)
{
byte[] fileBytes = TempData[id] as byte[];
return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name);
}
else
{
return null;
}
}
This works flawlessly in Google Chrome and Firefox browsers. 在Google Chrome浏览器和Firefox浏览器中,它可以完美运行。 However, when using either Internet Explorer or Microsoft Edge browsers, the file refuses to download. 但是,使用Internet Explorer或Microsoft Edge浏览器时,该文件拒绝下载。
The debug console doesn't produce any useful errors. 调试控制台不会产生任何有用的错误。 I have tried changing the returned File type to an octet stream and using window.location.href instead of a get request to download the file, but nothing appears to work. 我尝试将返回的File类型更改为八位字节流,并使用window.location.href而不是get请求来下载文件,但是似乎没有任何效果。 All of the functions are called and data passed between them correctly, so routes are not the problem. 调用了所有功能,并且在它们之间正确地传递了数据,因此路由不是问题。
Does anyone know how I can make the returned FileResult download? 有人知道我如何下载返回的FileResult吗?
Here is a solution. 这是一个解决方案。 It uses the same code as in my question except for the changes listed here. 它使用与我的问题相同的代码,但此处列出的更改除外。
Add an iframe element to your webpage: 将iframe元素添加到您的网页:
<iframe id="iFrameFileDownload" style="display: none;"></iframe>
In the javascript, instead of a call using $http.get(), set the 'src' attribute of the iframe element to the controller function url: 在JavaScript中,而不是使用$ http.get()进行调用,而是将iframe元素的'src'属性设置为控制器函数url:
$.ajax({
type: 'POST',
data: myDataObject,
url: 'MyController/GenerateExcel/',
success: function(data) {
if (data.id != "") {
$("#iFrameFileDownload").attr("src", 'MyController/DownloadExcel?id=' + encodeURIComponent(data.id) + '&name=' + encodeURIComponent(data.name));
return true;
}
}
});
Another solution that I considered is using the window.open() function instead of $http.get(). 我考虑的另一个解决方案是使用window.open()函数而不是$ http.get()。 (source: Download a file with mvc ) However, that solution uses popups and would require users to enable popups in their browser before downloading the file. (源: 使用mvc下载文件 )但是,该解决方案使用弹出窗口,并且要求用户在下载文件之前在浏览器中启用弹出窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.