这就是我从我的数据库中检索 varbinary
pdf 并在我的 asp.net 页面中查看的方式,
byte[] pdfFile= null;
var obj = DBContext.MyTable.Where(x => x.ID == 1 ).SingleOrDefault().pdfColumn;
pdfFile = new byte[obj.Length];
if (pdfFile!= null)
{
Response.ClearHeaders();
Response.Clear();
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", pdfFile.Length.ToString());
Response.AddHeader("Content-Disposition", "inline; filename=sample.pdf");
Response.BinaryWrite(pdfFile);
Response.Flush();
Response.End();
}
当我使用这段代码查看我的 pdf 时,我收到了这条消息
我的 obj
返回喜欢
字节数组的内容(pdfFile
)返回喜欢
我的 pdf 文件没有显示!我的代码有什么问题?
最佳答案
在我看来,您只是在创建一个具有正确长度的 obj 的字节数组,但您实际上并没有将 obj 的内容放入 pdfFile 数组。
如果你......会发生什么
byte[] pdfFile= null;
var obj = DBContext.MyTable.Where(x => x.ID == 1 ).SingleOrDefault().pdfColumn;
pdfFile = new byte[obj.Length];
obj.CopyTo(pdfFile);
您看到的错误和显示的空白页通常是 pdf 文件全部为二进制零的症状,或者如果您使用的是流,则流位置位于末尾。通过在 Debug模式下运行并在将字节数组或流发送到管道之前检查其内容,您可以轻松确定是否属于这种情况。检查长度和内容,它应该看起来像二进制,而不是全为零!
关于c# - 来自数据库的 varbinary pdf 已崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911116/