I am writing an application in MVC4.
I have a physical pdf file on the server. I want to convert this to a memory stream and send it back to the user like this:
return File(stream, "application/pdf", "myPDF.pdf");
But how do I convert a pdf file to a memory stream?
Thanks!
First create FileStream to open a file for reading. Then call FileStream. Read in a loop until the whole file is read. Finally close the stream.
Choose Tools, click on Edit PDF, then on Link, and finally, select Add/Edit Web or Document Link. Select the area that you want to hyperlink. Then, in the Create Link dialog box, choose the options you want for the link appearance and click on the Open a Web Page button for the link action. Hit Next and enter the link.
Open the file you want to convert in your PDF editor. Select the Create & Edit button on the right-side toolbar. Click Export PDF at the top of the window. Choose HTML Web Page and select your options.
You don't need MemoryStream
. Easiest way is to use overload that accepts file name:
return File(@"C:\MyFile.pdf", "application/pdf");
another solution is to use overload that accepts byte[]
:
return File(System.IO.File.ReadAllBytes(@"C:\Myfile.pdf"), "application/pdf");
or if you want use FileStream
:
return File(new FileStream(@"C:\MyFile.pdf", FileMode.Open, FileAccess.Read), "application/pdf");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With