c# - ASP.NET MVC:大 PDF 下载失败,但大 zip 下载成功
问题描述
我编写了一个代码来下载大型(大约 160 MB)PDF 文件。我可以在本地环境中下载大型 PDF 文件而没有任何问题,但在生产环境中同样失败。应用程序还具有压缩文件然后下载的功能,现在这在生产中完美运行。我可以下载 PDF 文件的 zip(zip 文件大小:157 MB),并且 zip 的大小与 PDF 文件的大小大致相同。错误:一旦我单击链接,PDF 文件下载就会开始,然后它会在 chrome 浏览器中出现消息“网络错误”而中断。在 IE 浏览器中的消息是:“test.pdf 下载被中断”。我进行了很多搜索,但找不到与我的问题相关的内容。任何人都可以帮助找出为什么它在消息“网络错误”之间失败的问题吗?
以下是我下载 PDF 文件时的代码:
//------ this code goes into web tier ------------
public FileResult DownloadFile(int FilePk, string fileName)
{
try
{
//Clear Browser Header Cache
var browserInformation = Request.Browser;
Response.ClearHeaders();
//Set as private if current browser type is IE
Response.AppendHeader("cache-control", browserInformation.Browser == "IE" ? "private" : "no-cache");
var pdfFileByte = MyWCFService.GetFile(FilePk); // this service returns byte[]
return statement.IsNull() ? null : File(pdfFileByte, "Application/pdf", "File-" + "fileName" + ".pdf");
}
catch (Exception ex)
{
OnException(ex);
}
return null;
}
//----- this code goes into wcf service which is on app tier---
public byte[] GetFile(int FilePk)
{
// Here decalre string filepath variable and get value into it using path present in database
var fi = new FileInfo(filePath);
if (!fi.Exists) return null;
var fileSize = (int)fi.Length;
var fileInByte = new byte[fileSize];
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fs.Read(fileInByte, 0, fileSize);
}
return fileInByte;
}
以下是我下载 zip 文件时的代码:
//------ this code goes into web tier ------------
public FileResult DownloadFileForAll(int exrqPk, string userFileName)
{
try
{
//Clear Browser Header Cache
var browserInformation = Request.Browser;
Response.ClearHeaders();
//Set as private if current browser type is IE
Response.AppendHeader("cache-control", browserInformation.Browser == "IE" ? "private" : "no-cache");
var statement = MyWCFService.GetFileAll(exrqPk);
return statement.IsNull() ? null : File(statement, "application/zip", userFileName + ".zip");
}
catch (Exception e)
{
OnException(e);
}
return null;
}
----- this code goes into wcf service which is on app tier---
public byte[] GetFileAll(int exrqPk)
{
// Here decalre string filepath variable and get value into it using path present in database
var fi = new FileInfo(filePath);
if (!fi.Exists) return null;
var fileSize = (int)fi.Length;
var fileInByte = new byte[fileSize];
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fs.Read(fileInByte, 0, fileSize);
}
return fileInByte;
}
解决方案
推荐阅读
- angular - Angular Material Datatable 未对数据进行排序
- odbc - SAP HANA ODBC 驱动程序损坏
- swift - 如何将 myObject 转换为 AnyPublisher
? - html - 背景图片不覆盖网页背景
- java - Chaquopy:将 Java ArrayList/LinkedList 转换为 Python 数组
- python - 在 Python 请求中获得 403 响应
- css - CSS:水平线,部分较粗
- c++ - How can I use a changing int value to call different functions on an Arduino?
- amazon-web-services - 如何将 CloudFormation 参数作为类型传递:数字?
- reactjs - React:如何在某些特定状态更改时停止重新渲染组件?