首页 > 解决方案 > 无法下载 PDF 文件,从 Sql 数据库中获取。已获取字节值,函数未出现任何错误,但未下载 PDF

问题描述

我无法下载从 Sql 数据库获取的 PDF 文件。字节值已被提取,函数没有抛出任何错误,但没有下载 PDF。

代码:

    public ActionResult PrintPDF(string projectSelection)
    {


        byte[] extract = (byte[])_selectionManager.FindPdf();

        MemoryStream pdfStream = new MemoryStream();
        pdfStream.Write(extract, 0, extract.Length);
        pdfStream.Position = 0;
        HttpContext.Response.AddHeader("content-disposition",
        "attachment; filename=form.pdf");

        return new FileStreamResult(pdfStream, "application/pdf");


    }

标签: model-view-controller

解决方案


既然您已经将文件内容作为字节,为什么将其转换为流?

请检查这是否有效

byte[] extract = (byte[])_selectionManager.FindPdf();
return File(
    extract, System.Net.Mime.MediaTypeNames.Application.Pdf, "form.pdf");

推荐阅读