首页 > 解决方案 > 从 SQL Server 中的 VARBINARY(MAX) 下载 .PDF

问题描述

我可以从下面的代码中下载 PDF,但是,当我尝试打开 pdf 文件时,会提到“PDF 已损坏”。这是什么问题?

在此处输入图像描述

我在用

byte[] binFile = File.ReadAllBytes(file_path_with_filename_with_ext);

作为通过存储过程传递的 SQL 参数。

C# 代码

[HttpGet]
public IActionResult DownLoadPdf(int? ID)
{
    string sql = @"SELECT ID, PDFReport
                   FROM [TABLE]";    // which is VARBINARY(MAX) value

    byte[] Report = _dbContext.PDFdto.FromSql(sql).AsNoTracking().Where(r => r.ID == ID.ToString()).Select(r => r.PDFReport).FirstOrDefault();

    return Json(new { Report });
}

HTML:

$scope.loadPdfClick = function (ID) {
$http({
    url: '@Url.Content("~/Report/DownLoadPdf")',
    method: "GET",
    params: {
        "ID": ID
    },
    headers: {
        'Content-type': 'application/json'
    },
    responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
    var blob = new Blob([data], { type: "application/pdf" });
    var objectUrl = URL.createObjectURL(blob);
    var a = document.createElement('a');
    document.body.appendChild(a);
    a.setAttribute('style', 'display:none');
    a.setAttribute('href', objectUrl);
    var filename = "Report_Test.pdf";
    a.setAttribute('download', filename);
    a.click();
    URL.revokeObjectURL(objectUrl);
}).error(function (data, status, headers, config) {
    messageAlert("ERROR");
});

PS:使用 .NET Core 1.1 和 Angular

标签: sql-serverangular.net-coreasp.net-core-mvcrestful-url

解决方案


推荐阅读