How to Display blob (.pdf) in an AngularJS app

后端 未结 8 1434
逝去的感伤
逝去的感伤 2020-11-22 11:01

I have been trying to display pdf file which I am getting as a blob from a $http.post response. The pdf must be displayed within the app using

8条回答
  •  猫巷女王i
    2020-11-22 11:48

    First of all you need to set the responseType to arraybuffer. This is required if you want to create a blob of your data. See Sending_and_Receiving_Binary_Data. So your code will look like this:

    $http.post('/postUrlHere',{myParams}, {responseType:'arraybuffer'})
      .success(function (response) {
           var file = new Blob([response], {type: 'application/pdf'});
           var fileURL = URL.createObjectURL(file);
    });
    

    The next part is, you need to use the $sce service to make angular trust your url. This can be done in this way:

    $scope.content = $sce.trustAsResourceUrl(fileURL);
    

    Do not forget to inject the $sce service.

    If this is all done you can now embed your pdf:

    
    

提交回复
热议问题