使用場景: 后台傳給前端一個圖片二進制流,但是要添加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即為圖片地址 });