vue 文件(图片,视频,word,pdf等)批量下载

在这里插入图片描述
需求:
多选之后点击批量下载,下载不同类型的文件,有各种格式的图片,有word文档,有pdf等。
我用的iview 框架的table组件。
直接上代码

// script 中的代码
export const downloadFile = url => {
  const iframe = document.createElement("iframe");
  iframe.style.display = "none";
  iframe.style.height = 0;
  iframe.src = url;
  document.body.appendChild(iframe);

  setTimeout(() => {
    iframe.remove();
  }, 5 * 60 * 1000);
};
// methods中的代码
bulkDownload() {
      var that = this;
      console.log(that.AllDownList);
      if (that.AllDownList.length == 0) {
        this.$Message.warning("请选择要下载的课件");
      } else {
        for (let i = 0; i < this.AllDownList.length; i++) {Ï
          // let name = this.AllDownList[i].courseName;
          let url =
            axios.defaults.baseURL +
            "/file/get?fileId=" +
            this.AllDownList[i].fileName;
            //判断数据中是什么类型的文件。courseCategory是项目自己定义的文件类型
          if (this.AllDownList[i].courseCategory == "image") {
            this.downloadIamge(url, this.AllDownList[i].courseName);
          } else {
            downloadFile(url);
          }
        }
		// 取消多选
        this.$refs.selection.selectAll(false);
        
      }
    },
	 downloadIamge(imgsrc, name) {
      //下载图片地址和图片名
      var image = new Image();
      // 解决跨域 Canvas 污染问题
      image.setAttribute("crossOrigin", "anonymous");
      image.onload = function() {
        var canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据

        var a = document.createElement("a"); // 生成一个a元素
        var event = new MouseEvent("click"); // 创建一个单击事件
        a.download = name || "photo"; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
      };
      image.src = imgsrc;
    }


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要根据文件路径在Vue中实现pdfword文件的预览,你可以使用一些第三方库来处理不同类型的文件。 对于PDF文件,可以使用`vue-pdf`库来实现预览功能。以下是一个示例代码: 1. 安装`vue-pdf`库: ```bash npm install vue-pdf ``` 2. 在Vue组件中引入并使用`vue-pdf`库: ```vue <template> <div> <pdf :src="pdfPath" :page="1"></pdf> </div> </template> <script> import { pdf } from 'vue-pdf'; export default { components: { pdf }, data() { return { pdfPath: '/path/to/your/file.pdf' }; } }; </script> ``` 通过设置`pdfPath`变量来指定PDF文件的路径,然后使用`<pdf>`组件来展示PDF文件的预览。 对于Word文件,可以使用`mammoth.js`库来提取Word文档的原始内容并在页面上展示。以下是一个示例代码: 1. 安装`mammoth.js`库: ```bash npm install mammoth ``` 2. 在Vue组件中引入并使用`mammoth.js`库: ```vue <template> <div> <div v-html="wordContent"></div> </div> </template> <script> import * as mammoth from 'mammoth'; export default { data() { return { wordPath: '/path/to/your/file.docx', wordContent: '' }; }, mounted() { this.previewWord(); }, methods: { previewWord() { axios.get(this.wordPath, { responseType: 'arraybuffer' }) .then(response => { const arrayBuffer = response.data; mammoth.extractRawText({ arrayBuffer }) .then(result => { this.wordContent = result.value; }) .catch(error => { console.error(error); }); }) .catch(error => { console.error(error); }); } } }; </script> ``` 通过设置`wordPath`变量来指定Word文件的路径,然后使用`mammoth.js`库提取文档的原始内容,并使用`v-html`指令将内容展示在页面上。 以上是根据文件路径在Vue中实现pdfword文件预览的示例代码。你可以根据实际需求进行修改和扩展。请确保你已经安装了相关的第三方库,并根据具体文件路径进行相应的处理和展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值