如何将PDF转换为内存流转换为、如何将、内存、PDF

2023-09-06 11:22:30 作者:生来就爱笑

我写在MVC4的应用程序。

I am writing an application in MVC4.

我在服务器上的物理pdf文件。我想将其转换为一个内存流并寄回给用户,像这样

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 so

返回文件(流应用程序/ PDF格式,myPDF.pdf);

return File(stream, "application/pdf", "myPDF.pdf");

但我怎么转换PDF文件到内存流?

but how do I convert a pdf file to a memory stream?

感谢

推荐答案

您不必的MemoryStream 。最简单的方法是使用过载接受的文件名:

You don't need MemoryStream. Easiest way is to use overload that accepts file name:

return File(@"C:\MyFile.pdf", "application/pdf");

另一种解决方案是使用重载,接受字节[]

return File(System.IO.File.ReadAllBytes(@"C:\Myfile.pdf"), "application/pdf");

如果你想使用的FileStream

return File(new FileStream(@"C:\MyFile.pdf", FileMode.Open, FileAccess.Read), "application/pdf");