6

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.

2
  • You need to actually display it in a div or just open it? Commented Apr 12, 2014 at 19:38
  • thanks for answer.I need to display it in a div. Commented Apr 12, 2014 at 19:38

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

6 Comments

I tried your code it did not work . do you have any idea why does not work?
PDFFile has byte so how can i display pdf file in view ?
@CMonster Interesting, I've never seen that. It works though?
It works!!! thanks but i have one more question.In below side of pdf , there are zoom in zoom out etc etc(menu).How can i hide menu ?
@user3527568: Happy I helped you, you can accept the answer if you'd like to anyways ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.