简体   繁体   English

来自数据库的pdf文件在asp.net mvc中查看

[英]Pdf file from database to view in asp.net mvc

I have Image column in Ms Sql Database.Pdf files are in that column. 我在Ms Sql Database中有Image列.Pdf文件在该列中。

public ActionResult Index()
{
DatabaseEntities _ context = new DatabaseEntities();


var PdfFile = _context.FileTable.where(p=>p.Id==1).Select(s=>s.FileData).FirstOrDefault();


return view();
}

I select file's Byte and set it to " var PdfFile" 我选择文件的字节并将其设置为“ var PdfFile”

But i am not sure how can i call PdfFile in view and display inside html div in asp.net mvc ? 但我不知道如何在视图中调用PdfFile并在asp.net mvc中的html div中显示?

Any help will be greatly appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

You have make use of ViewData. 您已经使用了ViewData。 Although I'm not sure how exactly you'er gonna display the pdf but to answer the question add this in your method: 虽然我不确定你将如何显示pdf,但要回答问题,请在您的方法中添加:

 ViewData["PDF"] = PDFFile; 

and in the razor you can get it this way: 在剃须刀中你可以这样得到它:

@var getData = ViewData["PDF"];

To Display it, first convert it to base64: 要显示它,首先将其转换为base64:

<object data="data:application/pdf;base64,@System.Convert.ToBase64String((Byte[])ViewData["PDF"])" type="application/pdf" width="500px">
<embed src="data:application/pdf;base64, @System.Convert.ToBase64String((Byte[])ViewData["PDF"])" type="application/pdf" />
</object>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM