vue elementUi中uolad文件上傳和v-viewer相結合實現圖片的預覽下載和刪除功能


1.首先安裝element ui 和 v-viewer(可以使用cnpm)

  npm i element-ui -S

  npm i v-viewer -S

2.全局配置element ui 和 v-viewer(main.js中)

  import ElementUI from 'element-ui'

  import 'element-ui/lib/theme-chalk/index.css'  
  Vue.use(ElementUI)

  import Viewer from 'v-viewer'
  import 'viewerjs/dist/viewer.css'
  Vue.use(Viewer, {
    defaultOptions: {
      zIndex: 9999
    }
  })

3.頁面中使用

<template>
  <div>
    <el-upload action="action" ref="upload" list-type="picture-card" :auto-upload="false"
    accept=".jpg, .jpeg, .png, .gif, .bmp, .JPG, .JPEG, .PBG, .GIF, .BMP" > 
      <i slot="default" class="el-icon-plus"></i>
      <div style="width: 100%; height: 100%; text-align: center;" slot="file" slot-scope="{file}">
        <div style="width: 100%; height: 100%;" class="images" v-viewer="{movable: false}">
          <img style="max-width: 100%; max-height: 100%;" :src="file.url">
        </div>
        <span class="el-upload-list__item-actions">
          <span title="預覽圖片" class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
            <i class="el-icon-zoom-in"></i>
          </span>
          <span title="下載圖片" class="el-upload-list__item-delete" @click="handleDownload(file)">
            <i class="el-icon-download"></i>
          </span>
          <span title="刪除圖片" class="el-upload-list__item-delete" @click="handleRemove(file)">
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </div>
    </el-upload>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        action: '',
      };
    },
    methods: {
      // 預覽
      handlePictureCardPreview(file) {
        for(let i = 0; i < this.$el.querySelectorAll('.images').length; i++) {
          if(file.url === this.$el.querySelectorAll('.images')[i].getElementsByTagName("img")[0].src) {
            const viewer = this.$el.querySelectorAll('.images')[i].$viewer;
            viewer.show();
            break;
          }
        }
      },
      // 下載
      handleDownload(file) {
        let a = document.createElement('a')
                  let image = new Image();
        // 解決跨域 Canvas 污染問題
        image.setAttribute("crossOrigin", "anonymous");
        image.onload = function() {
          let canvas = document.createElement("canvas");
          canvas.width = image.width;
          canvas.height = image.height;
          let context = canvas.getContext("2d");
          context.drawImage(image, 0, 0, image.width, image.height);
          let url = canvas.toDataURL("image/png"); //得到圖片的base64編碼數據
          let a = document.createElement("a"); // 生成一個a元素
          let event = new MouseEvent("click"); // 創建一個單擊事件
          a.download = file.name || "photo"; // 設置圖片名稱
          a.href = url; // 將生成的URL設置為a.href屬性
          a.dispatchEvent(event); // 觸發a的單擊事件
        };
        image.src = file.url;
        this.$message({
          message: '圖片下載成功',
          type: 'success'
        });
      },
      // 移除
      handleRemove(file) {
        this.$refs.upload.handleRemove(file);
      },
    }
  }
</script>

 

 預覽功能

 

 下載功能


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM