后端返回图片乱码,前端解决方法

发布时间 2023-09-14 16:49:41作者: 埃菲尔上的加菲猫

后端返回的文件流格式

 

 前端解决

axios({
        url:"/api/Component/ins/downloadQr?codes=" + data.codeIdentification,
        method:"GET",
        headers:{
            "Authorization": window.platToken.token_type +" "+window.platToken.access_token,
        },
        responseType:'blob'
    })
    .then(res=>{
        console.log('------------------------------');
        console.log(res.data);
        
            const src = window.URL.createObjectURL(new Blob([res.data],{type: 'application/octet-stream' }))
            const img = document.querySelector('#componentQRcode');
            console.log(img, src);
            img.src = src
    })
    .catch(err=>{
        console.log(err);
    })

  如果使用jquery,可能转换后还是不显示,改为使用axios,因为jq是比较老的,处理流数据类型,可能会做二次处理