首页 > 解决方案 > URL.createObjectURL(blob);

问题描述

我正在创建 PDF 并在新浏览器中打开。我的问题是单词“blob:”被附加到 URL

斑点:http://localhost:3000/a0b859c9-57a0-40b7-a60f-9d2a72ab3c14

我想成为'http://localhost:3000/a0b859c9-57a0-40b7-a60f-9d2a72ab3c14'

有没有办法我可以做到这一点?我的代码如下

 const blob = new Blob([response.data], {type : 'application/pdf'});
 var pdfFileUrl = URL.createObjectURL(blob);
 window.open(pdfFileUrl);
 URL.revokeObjectURL(pdfFileUrl);


update:
I tried below code as well. It also gives the same results.

''
const xhr = new XMLHttpRequest();

    //Send the proper header information along with the request


    // listen for `onload` event
    xhr.onload = () => {
        // process response
        if (xhr.status == 200) {
            // parse JSON data
           // console.log( "Response Received")
           // var pdfurl = URL.createObjectURL(xhr.response) ;
           console.log(xhr.response)
           window.open(URL.createObjectURL(xhr.response));

        
        } else {
            console.log(" In Error block")
            console.error('Error!');
        }
    };
    
    // create a `GET` request
    xhr.open('POST', url);
    xhr.responseType = 'blob'
    xhr.setRequestHeader('Content-Type', 'application/json');
    
    // send request
    xhr.send(JSON.stringify(payload));''
  

标签: javascriptpdfbrowserblob

解决方案


推荐阅读