問題描述: 調用后端接口,直接返回的是二維碼圖片,打印顯示為亂碼,前端如何處理數據后顯示到img標簽中
解決:
1.先設置axios接收參數格式為"arraybuffer"。responseType: 'arraybuffer'
2.轉換為base64格式圖片數據在img標簽顯示
axios.get(url地址, { responseType: 'arraybuffer', headers: { 'Authorization':localStorage.getItem('access_token') || '' } }) .then(response => { return 'data:image/png;base64,' + btoa( new Uint8Array(response.data) .reduce((data, byte) => data + String.fromCharCode(byte), '') ); }).then(data => { this.qrcode = data })
然后把返回的數據放在img標簽的src顯示