simpleUploader.vue 4.2 KB
Newer Older
XuanDai's avatar
XuanDai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<template>
  <uploader
    :options="options"
    :file-status-text="statusText"
    :autoStart="false"
    @file-added="fileAdded"
    @file-progress="onFileProgress"
    @file-success="onFileSuccess"
    @file-error="onFileError"
    class="uploader-example"
  >
    <uploader-unsupport></uploader-unsupport>
    <uploader-drop>
      <p>拖拽文件至此或点击</p>
      <uploader-btn>选择文件</uploader-btn>
    </uploader-drop>
    <uploader-list></uploader-list>
  </uploader>
</template>

<script>
var notUploadedChunks = []; // 已经上传过的文件chunkNumber数组
var isUploaded = false; // 文件已经上传成功了
import { mapGetters } from "vuex";
import { checkFileMd5,mergeFileMd5 } from "@/api/simpleUploader";
import SparkMD5 from "spark-md5";
const path = process.env.VUE_APP_BASE_API;
export default {
  name: "simpleUploader",
  data(){
    return{
      md5:""
    }
  },
  computed: {
    ...mapGetters("user", ["userInfo", "token"]),
    statusText() {
      return {
        success: "成功了",
        error: "出错了",
        uploading: "上传中",
        paused: "暂停中",
        waiting: "等待中"
      };
    },
    options() {
      return {
        target: path + "/simpleUploader/upload",
        testChunks: false,
        simultaneousUploads: 5,
        chunkSize: 2 * 1024 * 1024,
        headers: {
          "x-token": this.token,
          "x-user-id": this.userInfo.ID
        },
        checkChunkUploadedByResponse(chunk) {
          if (isUploaded) {
            return true; // return true 会忽略当前文件,不会再发送给后台
          } else {
              // 根据已经上传过的切片来进行忽略
              return (
                notUploadedChunks &&
                notUploadedChunks.some(
                  item => item.chunkNumber == chunk.offset + 1
                )
              );
          }
        }
      };
    }
  },
  methods: {

    // 上传单个文件
    fileAdded(file) {
      this.computeMD5(file); // 生成MD5
    },
    // 计算MD5值
    computeMD5(file) {
      var that = this;
      isUploaded = false; // 这个文件是否已经上传成功过
      notUploadedChunks = []; // 未成功的chunkNumber
      var fileReader = new FileReader();
      var md5 = "";

      file.pause();

      fileReader.readAsArrayBuffer(file.file);
      fileReader.onload = async function(e) {
        if (file.size != e.target.result.byteLength) {
          this.error(
            "Browser reported success but could not read the file until the end."
          );
          return false;
        }
        md5 = SparkMD5.ArrayBuffer.hash(e.target.result, false);

        file.uniqueIdentifier = md5;
        if (md5 != "") {
          const res = await checkFileMd5({ md5: md5 });
          if (res.code == 0) {
            if (res.data.isDone) {
              // 上传成功过
              isUploaded = true;
              that.$message({
                message: "该文件已经上传成功过了,秒传成功。",
                type: "success"
              });

              file.cancel();
            } else {
              isUploaded = false;
              notUploadedChunks = res.data.chunks;
              if(notUploadedChunks.length){
                file.resume();
              }
            }
          }
        }

        
      };
      fileReader.onerror = function() {
        this.error(
          "generater md5 时FileReader异步读取文件出错了,FileReader onerror was triggered, maybe the browser aborted due to high memory usage."
        );
        return false;
      };
    },
    // 上传进度
    onFileProgress() {},
    // 上传成功
    async onFileSuccess(rootFile, file) {
      await mergeFileMd5({md5:file.uniqueIdentifier,fileName:file.name})
    },
    onFileError(rootFile, file, response) {
      this.$message({
        message: response,
        type: "error"
      });
    }
  }
};
</script>

<style>
.uploader-example {
  width: 880px;
  padding: 15px;
  margin: 115px 15px 20px;
  font-size: 12px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
}
.uploader-example .uploader-btn {
  margin-right: 4px;
}
.uploader-example .uploader-list {
  max-height: 440px;
  overflow: auto;
  overflow-x: hidden;
  overflow-y: auto;
}
</style>