Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pdf file from database to view in asp.net mvc

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.

like image 964
user3527568 Avatar asked Apr 12 '14 19:04

user3527568


1 Answers

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>
like image 79
Transcendent Avatar answered Oct 07 '22 18:10

Transcendent