文件下载问题-ASP.NET MVC应用程序
我在 ASP.NET MVC 应用程序的视图中有两个控制器操作和 js,用于生成和下载 Excel 文件。所有相关代码都显示在这里:
public ActionResult GetExcel()
{
string handle = Guid.NewGuid().ToString();
using (MemoryStream MemoryStream = new MemoryStream())
{
//GetExcelData returns an OfficeOpenXml.ExcelPackage object
GetExcelData().SaveAs(MemoryStream);
MemoryStream.Position = 0;
TempData[handle] = MemoryStream.ToArray();
}
return new JsonResult()
{
Data = new { FileGuid = handle, FileName = "FileName" + DateTime.Now.ToString() + ".xlsx" }
};
}
public virtual ActionResult Download(string fileGuid, string fileName)
{
if (TempData[fileGuid] != null)
{
byte[] data = TempData[fileGuid] as byte[];
return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
}
else
{
return new EmptyResult();
}
}
$.ajax({
url: '@Url.Action("GetExcel", "Products")',
type: 'POST',
}).done(function (data) {
window.location = '@Url.Action("Download", "Products")' + '?fileGuid=' + data.FileGuid + '&filename=' + data.FileName;
});
我的问题是此代码会导致不一致的行为。
有时它会按预期工作,并且系统会提示我下载文件屏幕,但有时它会导致一个空白页面,其中文件的路径作为浏览器的 URL。
我注意到的一件事是,当问题发生时,浏览器网络选项卡上的文档类型是普通的,而不是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
,正如预期的那样。
还有一点是,这种情况发生在应用程序位于服务器上的 IIS 上时。在开发环境中本地不会发生这种情况。
此外,显然,这似乎与文件大小无关,因为它发生在小型和大型数据集上。
I have two controller actions and js in a view in an ASP.NET MVC application to generate and download an Excel file. All relevant code is shown here:
public ActionResult GetExcel()
{
string handle = Guid.NewGuid().ToString();
using (MemoryStream MemoryStream = new MemoryStream())
{
//GetExcelData returns an OfficeOpenXml.ExcelPackage object
GetExcelData().SaveAs(MemoryStream);
MemoryStream.Position = 0;
TempData[handle] = MemoryStream.ToArray();
}
return new JsonResult()
{
Data = new { FileGuid = handle, FileName = "FileName" + DateTime.Now.ToString() + ".xlsx" }
};
}
public virtual ActionResult Download(string fileGuid, string fileName)
{
if (TempData[fileGuid] != null)
{
byte[] data = TempData[fileGuid] as byte[];
return File(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
}
else
{
return new EmptyResult();
}
}
$.ajax({
url: '@Url.Action("GetExcel", "Products")',
type: 'POST',
}).done(function (data) {
window.location = '@Url.Action("Download", "Products")' + '?fileGuid=' + data.FileGuid + '&filename=' + data.FileName;
});
My problem is that this code results on an inconsistent behavior.
Sometimes it works as expected and I get to be prompted with a download file screen, but sometimes it results in a blank page with the path to the file as browser's url.
One thing I noticed is that when the problem happens, the type of the document on browser's network tab is plain, not application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
, as it would be expected.
Another point is that this happens just when the application is on the IIS on the server. This does not happen locally in development environment.
Also, apparently, is does not seems to be related to file size, since it is happening with small and large data sets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 EPPlus 库生成并下载 Excel 文件。从管理 Nuget 包安装 EPPLUS。确保您使用该库,然后在 NonCommercial LicenseContext 下使用它,如 ExcelPackage.LicenseContext = LicenseContext.NonCommercial
如果您想直接通过锚标记链接“@Url.Action("Test","Test")”生成,则使用以下代码。
如果您发送 ajax 请求,则代码将相同,直到 excel.Save();只需更改响应的返回类型,例如 JsonResult
您遇到的问题可能与文件大小有关只需尝试将响应包装在具有最大长度的 JSON 中
Ajax 请求从服务器返回的 JSONResult 响应下载文件对象在成功函数中
I have used EPPlus library to generate and download an excel file. Install EPPLUS from Manage Nuget Packages. Make sure if you use the library then use it under NonCommercial LicenseContext as ExcelPackage.LicenseContext = LicenseContext.NonCommercial
If you wanna generate directly through anchor tag link "@Url.Action("Test","Test")" then use the following code.
If you're sending an ajax request then code will be same till excel.Save(); just change the return type of the response such as JsonResult
The issue that you are experiencing might be related to the file size just try by wrapping the response in JSON with max length
Ajax Request to download the file object from the JSONResult response returned by the server in the success function