首页 > 解决方案 > 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;
    }

标签: c#asp.net-mvcpdfiis

解决方案


推荐阅读