vue 后端直接返回图片流,前端怎么直接显示在页面上

image.png
我网上搜了一下,说window.URL.createObjectURL(res)可以但是没有效果

fetchData() {
      const id = this.$route.query.companyId
      getCusInfo(id).then(res => {
        const _data = res.data
        this.detail = _data
        getFileDetail(_data.adAvatar).then(res => {
        //   const blob = new Blob([res.data], { type: 'image/png' })
        //   const url = window.URL.createObjectURL(blob)
        //   this.brandLogoUrl = url
        const src = window.URL.createObjectURL(res)
        this.brandLogoUrl = src
          console.log('res', res)
        })
        console.log('brandLogoUrl', this.brandLogoUrl)
      })
    },
<el-form-item label="广告头像" prop="adAvatar">
          <el-upload
            class="avatar-uploader"
            action="#"
            :http-request="advertiserUpload2"
            :show-file-list="false"
          >
            <img v-if="adAvatarUrl" :src="adAvatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

我暂时是这么处理的
image.png

阅读 18.9k
5 个回答

后端设置一下response type就行了,为什么前端要这么麻烦地去处理

直接把 API 地址丢到 src 就可以了。

后端设置 content-type: image/png 响应头 前端直接 img 标签展示即可

感觉这个后台是在给前端找麻烦的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题