How to convert a pdf to a memory stream

后端 未结 3 1074
自闭症患者
自闭症患者 2021-01-05 00:37

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:

相关标签:
3条回答
  • 2021-01-05 01:13

    Worked it out

     var pdfContent = new MemoryStream(System.IO.File.ReadAllBytes(imageLocation));
                    pdfContent.Position = 0;
                    return new FileStreamResult(pdfContent, "application/pdf");
    
    0 讨论(0)
  • 2021-01-05 01:21

    Use an overload that uses a filename, see here. It's the easiest solution when you have a physical file.

    0 讨论(0)
  • 2021-01-05 01:23

    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");
    
    0 讨论(0)
提交回复
热议问题