Pdf file from database to view in asp.net mvc

故事扮演 提交于 2020-02-21 12:37:06

问题


I have Image column in Ms Sql Database.Pdf files are in that column.

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"

But i am not sure how can i call PdfFile in view and display inside html div in asp.net mvc ?

Any help will be greatly appreciated.

Thanks.


回答1:


You have make use of ViewData. Although I'm not sure how exactly you'er gonna display the pdf but to answer the question add this in your method:

 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:

<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>


来源:https://stackoverflow.com/questions/23035187/pdf-file-from-database-to-view-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!