0

So iv come across this question time and time again in this forum, yet none of them cater for what i need.

  1. I'm using a code first approach, c# mvc5
  2. I'v managed to store a pdf to my database using a data type byte array.

I store pdf's to the database, the exact same way you would save a jpeg. I use file stream. My question is, how do i now display this pdf that has been stored. I'm completely lost...help please

MikeSW
  • 15,292
  • 3
  • 33
  • 50
Sanvir
  • 173
  • 1
  • 8
  • Select it out, save it and run it. What specifically are you having trouble with? – Blindy Jul 28 '15 at 18:03
  • Im not sure exactly how to view it(select it out), from the database in my c# application – Sanvir Jul 28 '15 at 18:09
  • possible duplicate of [Stream file using ASP.NET MVC FileContentResult in a browser with a name?](http://stackoverflow.com/questions/3206682/stream-file-using-asp-net-mvc-filecontentresult-in-a-browser-with-a-name) – Pluto Jul 28 '15 at 18:32

1 Answers1

0

So you already know how to make the file stream, just return a FileResult instead of a View and the client's browser will know how to display a PDF as seen here.

return File(stream, "application/pdf", "DownloadName.pdf");
Community
  • 1
  • 1
Crowcoder
  • 9,566
  • 3
  • 30
  • 39
  • Okay i understand what you saying. the link that you have seems to solve my problem. just a question, for "DownloadName.pdf", i replace this with the name of the field that the pdf is stored in my database. example, i am uploading pdf version of my payslip document...so this document is stored in my database in the field payslipdocument...so i replace "DownloadName.pdf" with "model.payslipdocument"? – Sanvir Jul 28 '15 at 19:05
  • @Sanvir no, that is [The file name to use in the file-download dialog box that is displayed in the browser.](https://msdn.microsoft.com/en-us/library/Dd492593(v=VS.118).aspx) – Crowcoder Jul 28 '15 at 19:13
  • @Sanvir you can name it anything, "DownloadName.pdf" is just an example. – Crowcoder Jul 29 '15 at 12:08
  • this line of code allows me to download the file return File(stream, "application/pdf", "DownloadName.pdf"); – Sanvir Aug 17 '15 at 22:04
  • for me to view it, i used return File(stream, "application/pdf"); and it opens up in my browser – Sanvir Aug 17 '15 at 22:05