sql-server - 从 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 - 无法通过 Reporting Services 配置管理器连接 Reporting Server
- jgit - 使用 JGit 通过 InMemoryRepository 和 SSH 获取 git repo
- numpy - Numpy排序工作奇怪
- qr-code - 改进 zbarimg 二维码识别
- angular - Bootstrap responsive 在 Angular 的资产文件夹中不起作用
- directus - 如何使用标准 directus 接口创建自定义接口?
- algorithm - 在实现队列的标准操作时使用双向链表有什么好处?
- flutter - 在构建方法中使用三元运算符
- pytorch - 拟合模型后 4D 数据变为 3D
- c - 为什么我们应该在声明函数的同一个文件中包含函数原型的头文件?