自学内容网 自学内容网

后端接口返回图片,前端的处理方法

接口返回如下图所示:

打印结果如下图所示:

出现问题的原因的axios默认返回的是json文本形式,二进制图片数据被强制转换成了 json 文本形式

处理方法:

首先,在axios中,将responseType默认返回数据类型json改为arraybuffer类型

export function downloadPro(data) {
    return request({
        url: '/aisynergy/api/v1/outboundScene/downloadFlowChart',
        method: 'post',
        responseType: 'arraybuffer',
        data
    })
}

然后,在页面中:

 downloadPro(formData).then(res => {
        this.picUrl= `data: image/jpeg;base64,${btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`
      })

原文地址:https://blog.csdn.net/qq_57039857/article/details/140635193

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!