1,下载图片预览返回的是二进制的字节流,如何与img标签中的src属性绑定,查看图片呢?
2.接口中加上响应类型: responseType: 'blob'
export function downloadFile(fileId) {
return service.request({
method: 'get',
url: `/file/download/${fileId}`,
responseType: 'blob'
})
}
3.URL.createObjectURL(下载接口中返回的二进制流)
async getDeta(id){
console.log(id)
this.modDialogVisible = true
const res = await getBannerDeta({bannerId:id})
this.deta = res.data
this.modInfo.attachment.attachId = this.deta.attachment.attachId
this.modInfo.bannerId = id
this.modInfo.bannerState = res.data.bannerState
this.modInfo.bannerTitle = res.data.bannerTitle
var fileId= res.data.attachment.url
const res1 = await downloadFile(fileId)
this.modurl = URL.createObjectURL(res1);
},
4.src绑定
<img v-if="modurl" :src="modurl" class="avatar">