从 MVC 应用程序下载 PDF 文件时出现问题

Issues when downloading PDF file from MVC application

我创建了一个用户可以从我的网页下载 pdf 文件的功能。该文件存储在数据库中,并从 webapi 请求。 webapi的return值是一个byte[].

我的问题是,当我 运行 本地 i 上的 Web 应用程序使用此功能 运行 时没有任何错误。我得到了 pdf 文件,它已正确下载到我的机器上。但是当我将 Web 应用程序部署到我的测试服务器时,此代码会在 chrome 中生成 RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION 以及其他文件下载到机器的一些文件但是当我尝试打开 pdf 文件时,我得到:无法加载 pdf 文件

chrome 和 IE 都会出现这种情况。

这是我的代码:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    this.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    var result = File(translationFile.FileContent.Content, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
    return result;
}

我已经尝试解决此问题 2 天了,但我就是无法弄清楚问题出在哪里。希望你们能帮忙。谢谢

您不需要使用 Content-Disposition。 .Net 会为您添加它。来自 docs.

The fileDownloadName parameter is used to generate the content-disposition header. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed. The MediaTypeNames class can be used to get the MIME type for a specific file name extension.

我倾向于使用 Stream-overload:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    var stream =  = new MemoryStream(translationFile.FileContent.Content);
    return File(stream, "application/pdf", fileName);
}

但是你也可以使用byte[]:

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    return File(translationFile.FileContent.Content, "application/pdf", fileName);
}

编辑: 如果您在打开 PDF 时遇到错误,您也可以通过手动从代码中保存 PDF 来确保 Web 浏览器做正确的事情。如果该文件也有错误,您可能生成了不正确的 byte[].

[HttpGet]
[DoNotChangeCacheSettings]
public virtual FileResult DownloadTranslationFile(Guid id)
{
    Guid assessmentTemplateId = id;
    File translationFile = Services.GetFileContent(assessmentTemplateId);
    var fileName = HttpUtility.UrlPathEncode(translationFile.FileName);
    var stream =  = new MemoryStream(translationFile.FileContent.Content);

    // Code for debugging
    var tempDir = "C:\temp"; // Make sure app pool can write here.
    var path = Path.Combine(tempDir, fileName); // Possibly add extension here.
    using (var fileStream = File.Create(path))
    {
        stream.Seek(0, SeekOrigin.Begin);
        stream.CopyTo(fileStream);
    }
    stream.Seek(0, SeekOrigin.Begin);

    // Return to client.
    return File(stream, "application/pdf", fileName);
}