Window.Open with PDF stream instead of PDF location

前端 未结 2 563
一向
一向 2020-12-13 18:43

Based on the question Open PDF in new browser full window, it looks like I can use JavaScript to open a new window with a PDF file with the following code:



        
2条回答
  •  鱼传尺愫
    2020-12-13 19:17

    It looks like window.open will take a Data URI as the location parameter.

    So you can open it like this from the question: Opening PDF String in new window with javascript:

    window.open("data:application/pdf;base64, " + base64EncodedPDF);
    

    Here's an runnable example in plunker, and sample pdf file that's already base64 encoded.

    Then on the server, you can convert the byte array to base64 encoding like this:

    string fileName = @"C:\TEMP\TEST.pdf";
    byte[] pdfByteArray = System.IO.File.ReadAllBytes(fileName);
    string base64EncodedPDF = System.Convert.ToBase64String(pdfByteArray);
    

    NOTE: This seems difficult to implement in IE because the URL length is prohibitively small for sending an entire PDF.

提交回复
热议问题