vue下载文件流生成图片

点击下载-----向后台发出请求-----后台返回图片的文件流-----转为图片-----下载图片

1. template 

<template>
  <div>   
    <el-button type="primary" size="mini" @click="downLoadImgs"
          >照片下载</el-button>

    <div v-for="infoImg in photoList" :key="infoImg.id">
      <el-checkbox-group v-model="checkList">
          <el-checkbox :label="infoImg">
               <img :src="infoImg.imageUrl" alt="" /> 
          </el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>

2.script

data() {
    return { 
      checkList: [], 
    };
  },
methods: {
    downLoadImgs() {
      this.$confirm("确定下载影像吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 单张图片循环下载
          this.checkList = this.checkList.map(({ path }) => {
            this.$request("downloadImg", { Key: path }).then((res) => {
              const blob = new Blob([res.data]);
              const fileName = "qudaoerweima.jpg";
              if ("download" in document.createElement("a")) {
                // 非IE下载
                const dlink = document.createElement("a");
                dlink.download = fileName;
                dlink.style.display = "none";
                dlink.href = URL.createObjectURL(blob);
                document.body.appendChild(dlink);
                dlink.click();
                URL.revokeObjectURL(dlink.href); // 释放URL 对象
                document.body.removeChild(dlink);
              } else {
                // IE10+下载
                navigator.msSaveBlob(blob, fileName);
              }
              this.$message.success("下载影像成功");
            });
          });
        })
        .catch(() => {});
    },
}

3.api.js

  
export default [
{
    /** 下载图片 **/
    name: "downloadImg",
    url: "/api/Download",
    method: "get",
    responseType: "blob", // 下载文件流
    // pem: true,
  },
]

4.结果

Vue中展示文件流图片,可按以下步骤实现: 1. **配置获取图片流接口**:使用`axios`发送请求,设置`responseType`为`blob`,以确保获取到的是文件流。示例代码如下: ```javascript import axios from 'axios'; export function getCurrent(dev) { return axios({ url: '/xxx', method: 'get', responseType: 'blob', params: { dev } }); } ``` 2. **在Vue组件中使用获取的图片流**:在组件中调用接口获取图片流,使用`Blob`对象和`URL.createObjectURL()`方法生成图片的URL,然后将其绑定到`img`标签的`src`属性上。示例代码如下: ```vue <template> <div class="home"> <el-image style="width: 200px; height: 200px" :src="imgUrl" :preview-src-list="srcList"></el-image> </div> </template> <script> import { getCurrent } from './api'; export default { data() { return { imgUrl: '', srcList: [] }; }, methods: { loadFile() { getCurrent('your_dev_value').then(res => { let blob = new Blob([res.data], { type: 'image/jpeg' }); const imageUrl = URL.createObjectURL(blob); this.imgUrl = imageUrl; this.srcList.push(imageUrl); }).catch(err => { console.log('获取图片失败'); }); } }, mounted() { this.loadFile(); } }; </script> ``` 在上述代码中,`getCurrent`函数用于获取图片流,`loadFile`方法在组件挂载时调用该函数,将获取到的图片流转换为URL,最后将URL绑定到`el-image`组件的`src`属性上,实现图片的展示。同时,将图片URL添加到`srcList`数组中,可用于图片预览功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值