vue 实现文件上传和本地预览

1、图片上传imgUpload.vue

<template>
  <div>
    <div>
      <input v-show="false" type="file" accept="image/*" @change="changeFile($event)" ref="input"/>
      <button @click="uploadImgs">上传图片</button>
      <div v-for="(item,index) in imgList" class="imgList" :key="Math.random()">
        <img style="max-height:100%;max-width:100%;" :src="item"/>
        <span class="delete" @click="deleteImg(index)"> X </span>
      </div>
      <div v-for="(item,index) in imgesList" class="imgList" :key="Math.random()">
        <img style="max-height:100%;max-width:100%;" :src="item"/>
        <span class="delete" @click="deleteImg(index)"> X </span>
      </div>
      <!--<input type="file" id="file"/>-->
      <!--<button type="button" value="读取二进制数据" @click="readAsBinaryString()"/>-->
      <!--<button type="button" value="读取文本文件" @click="readAsText()"/>-->
      <!--<div id="result" name="result"></div>-->
    </div>
  </div>
</template>
<script>
  import {uploadImg} from '../../../../apis/baseMessage/index'

  export default {
    data() {
      return {
        imgList: [],
        imgesList: [],
      };
    },
    methods: {
      // 上传
      uploadImgs() {
        this.$refs.input.click();
      },
      changeFile: function (e) {
        console.log("e", e)
        let file = e.target.files[0];
        //本地展示
        //把file对象/blob对象 转化为base64,可以预览
        var reader = new FileReader();
        reader.readAsDataURL(file);
        let _this = this;
        reader.onload = function (e) {
          _this.imgesList.push(this.result)
          // _this.imgesList.push( "data:image/png;base64," + this.result.substring(this.result.indexOf(",") + 1))
        };
        //上传至服务器后本地展示
        //在上传图片,基本采用的是上传file formData方式。一般上传blob对象和file对象都是可以的,但是
        //有些后台只能是 file对象方式,这个时候需要把blob对象转化为file对象方式。
        var forms = new FormData()
        forms.append('file', file)
        uploadImg(forms).then(res => {
          if (res.status == 200) {
            this.imgList.push(res.data.filePath)
          } else {

          }
        }).catch(res => {

        })
      },
      // 删除图片
      deleteImg(index) {
        this.imgList.splice(index, 1)
      },
      // readAsBinaryString() {
      //   var file = document.getElementById("file").files[0];
      //   var reader = new FileReader();
      //   //将文件以二进制形式读入页面
      //   reader.readAsBinaryString(file);
      //   reader.onload = function (e) {
      //     var result = document.getElementById("result");
      //     //显示文件
      //     result.innerHTML = this.result;
      //   }
      // },
      // readAsText() {
      //   var file = document.getElementById("file").files[0];
      //   var reader = new FileReader();
      //   //将文件以文本形式读入页面
      //   reader.readAsText(file);
      //   reader.onload = function (e) {
      //     var result = document.getElementById("result");
      //     //显示文件
      //     result.innerHTML = this.result;
      //   }
      // }


    }
  }

</script>
<style scoped>
  .imgList {
    width: 100px;
    height: 100px;
    border: 1px solid #dddddd;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: 10px;
  }

  .delete {
    cursor: pointer;
    position: absolute;
    right: -5px;
    top: -10px;
  }
</style>

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Vue中的Word模板在线预览本地文件,可以借助于第三方库`docxtemplater`和`jszip`。具体步骤如下: 1. 安装`docxtemplater`和`jszip`库: ```bash npm install docxtemplater jszip --save ``` 2. 在Vue组件中引入这两个库: ```javascript import Docxtemplater from 'docxtemplater'; import JSZip from 'jszip'; ``` 3. 在Vue组件中添加一个文件上传的input框和一个用于显示Word文档内容的div: ```html <template> <div> <input type="file" ref="fileInput" @change="onFileChange"> <div ref="wordContent"></div> </div> </template> ``` 4. 在Vue组件的methods中编写处理文件上传Word模板预览的方法: ```javascript methods: { onFileChange() { const file = this.$refs.fileInput.files[0]; const reader = new FileReader(); reader.onload = () => { this.previewWord(reader.result); }; reader.readAsArrayBuffer(file); }, previewWord(data) { JSZip.loadAsync(data).then((zip) => { const doc = new Docxtemplater().loadZip(zip); const result = doc.getZip().generate({type: 'blob'}); const wordContent = this.$refs.wordContent; wordContent.innerHTML = ''; const fileReader = new FileReader(); fileReader.onloadend = () => { const content = fileReader.result; const iframe = document.createElement('iframe'); iframe.src = `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,${btoa(content)}`; iframe.width = '100%'; iframe.height = '800px'; wordContent.appendChild(iframe); }; fileReader.readAsBinaryString(result); }); }, } ``` 这段代码的作用是: - 当用户选择文件后,将文件读取为二进制数组,并调用`previewWord`方法进行预览。 - 在`previewWord`方法中,使用`JSZip`将Word文档的二进制数组解压缩,然后使用`docxtemplater`加载Word文档模板。 - 调用`docxtemplater`的`getZip()`方法生成Word文档,并将其转换为二进制字符串。 - 将二进制字符串转换为base64编码,生成一个iframe元素,并将其作为子元素添加到Vue组件中用于显示Word内容的div中。 这样,当用户上传一个Word文档后,就可以在Vue组件中预览该文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值