vue axios從服務器加載圖片並顯示


使用場景: 后台傳給前端一個圖片二進制流,但是要添加httpp header,但是在傳統的用img標簽查看圖片,無法添加http header

    this.$axios({
           method: 'get',
           url: "img url" ,
           headers: {
             'Content-Type': 'application/x-www-form-urlencoded',
             "token": this.token  // 必須添加的請求頭
           },
           responseType: "arraybuffer", // 關鍵 設置 響應類型為二進制流
         }).then(function (response) {  // 將后台的圖片二進制流傳華為base64
           return 'data:image/png;base64,' + btoa(
             new Uint8Array(response.data).reduce((data, byte) => data + String.fromCharCode(byte), '')
           );
         }).then(data=>{
           _this.imgUrl=data; // data即為圖片地址
          });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM