将 Pdf 响应从 API 调用转换为 Blob 并生成 Blob Url

Convert Pdf response from API Call to Blob and generate Blob Url

我正在尝试将响应转换为 blob,然后生成 url 以访问它。 get 请求的响应是 Pdf.

这是我正在做的。

this.$http.get<string>(
        invoicePath
      ).then((response:any)=> {
        console.log("CREATING A BLOB")
        console.log("RESPONSE BLOB: ", response.data); 
        const blob:any = new Blob([response], { type: 'application/pdf; charset=utf-8' });
        console.log("RESPONSE BLOB: ", blob);
        const url= window.URL.createObjectURL(blob);
        // window.open(url);
        return url
        //window.location.href = response.url;
      })

返回的 url 给出了以下错误消息。

我们必须先将响应转换为 ArrayBuffer。

this.$http.get<string>(
        invoicePath, {responseType:'arraybuffer'}
      ).then((response:any)=> {
        console.log("CREATING A BLOB")
        console.log("RESPONSE BLOB: ", response.data); 
        const blob:any = new Blob([response.data], { type: 'application/pdf; charset=utf-8' });
        console.log("RESPONSE BLOB: ", blob);
        const url= window.URL.createObjectURL(blob);
        // window.open(url);
        return url
        //window.location.href = response.url;
      })