[removed] Open PDF in new tab from byte array

后端 未结 2 1031
甜味超标
甜味超标 2020-12-15 21:07

I\'m using AngularJS with an HTTP resource to call an external API and my response is a byte array. I need to turn this byte array into a PDF in a new window. I haven\'t see

相关标签:
2条回答
  • 2020-12-15 21:26

    If anyone still looks for that, here is what I'm doing (and working) :

    var pdfAsDataUri = "data:application/pdf;base64,"+byteArray;
    window.open(pdfAsDataUri);
    

    Where byteArray is the data you receive. It's maybe not a nice solution (byte array is visible in the URL), but it works...

    0 讨论(0)
  • 2020-12-15 21:30

    You would need to pass the responseType in your service call

    $http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
    

    then in the success of your data call this should open up pdf in a new window:-

        getDocument()
            .success(function(data) {
                var file = new Blob([data], { type: 'application/pdf' });
                var fileURL = URL.createObjectURL(file);
                window.open(fileURL);
        })
    

    From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael

    0 讨论(0)
提交回复
热议问题