user2206329 user2206329 - 1 year ago 114
C# Question

How to convert a pdf to a memory stream

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?


Answer Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download