Display PDF using an AJAX call

前端 未结 4 1726
广开言路
广开言路 2020-12-03 01:39

I\'m trying to display a PDF(which is created in the server side and pass to the client side as a web stream) through an AJAX call. My code is given below:

j         


        
4条回答
  •  不知归路
    2020-12-03 02:32

    For anybody that stumbles across this. Here is an example using axios

    1. responseType must be 'arrayBuffer'
    2. create a Blob object from response
    3. create an "object url" from the blob that you can load into the iframe

          axios({
              url: `path/to/pdf`,
              method: "GET",
              responseType: 'arraybuffer'
          }).then((response) => {
              let blob = new Blob([response.data], { type: response.headers['content-type'] } );
              let url = window.URL.createObjectURL(blob);
      
              $('#frame').attr('src',url);
          });
      

提交回复
热议问题