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