fuliqi
2024-07-10 8480b10b70bf09f204597441a1f8c2496c455863
src/components/UploadC.vue
@@ -1,13 +1,14 @@
<template>
  <div>
    <el-upload :action="uploadUrl" :show-file-list="true" :limit="uploadNum" :accept="acceptList" multiple
      :file-list="fileUrl" :on-remove="handleRemove" :before-remove="beforeRemove" :on-success="handleUploadSuccess"
      :before-upload="beforeUpload">
               :file-list="fileUrl" :on-remove="handleRemove" :before-remove="beforeRemove"
               :on-success="handleUploadSuccess"
               :before-upload="beforeUpload">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传pdf、mp4、avi、png、jpg、jpge文件,且不超过{{ fileSizeLimitM }}M</div>
      <div slot="tip" class="el-upload__tip">只能上传pdf、mp3、mp4、avi、webm、png、jpg、jpge、doc、docx、xls、xlsx、ppt、pptx文件,且不超过{{ fileSizeLimitM }}M</div>
      <div v-if="fileUrl && fileUrl.length > 0 && uploadNum === 1">
        <video controls class="returnShow" v-if="fileType === 'video'" :src="'/api/files/' + fileUrl[0].url"></video>
        <img class="returnShow" v-if="fileType === 'img'" :src="'/api/files/' + fileUrl[0].url" />
        <img class="returnShow" v-if="fileType === 'img'" :src="'/api/files/' + fileUrl[0].url"/>
      </div>
    </el-upload>
  </div>
@@ -16,7 +17,7 @@
<script>
export default {
  name: "UploadC",
  name: 'UploadC',
  props: {
    uploadNum: {
      required: false,
@@ -38,67 +39,73 @@
      default: () => []
    }
  },
  data() {
  data () {
    return {
      uploadUrl: "http://localhost:8000/api/upload/upload",
      uploadUrl: 'http://localhost:8000/api/upload/upload',
      fileTypeList: {
        'video': ['mp4', 'avi'],
        'video': ['mp4', 'avi', 'webm'],
        'img': ['jpg', 'png', 'jpeg'],
        'pdf': ['pdf'],
        'word': ['doc', 'docx'],
        'excel': ['xlsx', 'xls'],
        'audio': ['mp3'],
        'ppt': ['ppt', 'pptx'],
        'file': ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'png', 'jpg', 'jpeg', 'pdf'],
      }
    };
    }
  },
  methods: {
    clearFile() {
      this.fileUrl = [];
    clearFile () {
      this.fileUrl = []
    },
    beforeRemove(file, fileList) {
      if (file && file.status === "success") {
        return this.$confirm(`确定移除 ${file.name}?`);
    beforeRemove (file, fileList) {
      if (file && file.status === 'success') {
        return this.$confirm(`确定移除 ${file.name}?`)
      }
    },
    handleRemove(file, fileList) {
      this.$emit('removeFile', this.fileUrl, file.name);
    handleRemove (file, fileList) {
      this.$emit('removeFile', this.fileUrl, file.name)
    },
    handleUploadSuccess(res, file) {
      this.fileUrl.push(res.data);
      this.$emit('getUploadUrl', this.fileUrl);
    handleUploadSuccess (res, file) {
      this.fileUrl.push(res.data)
      this.$emit('getUploadUrl', this.fileUrl)
    },
    beforeUpload(file) {
      const { type } = file;
      const typeList = this.fileTypeList[this.fileType];
      let limitType = true;
    beforeUpload (file) {
      const fileName = file.name // 获取文件名
      const extension = fileName.split('.').pop() // 提取扩展名
      const typeList = this.fileTypeList[this.fileType]
      let limitType = true
      if (typeList) {
        const tempType = typeList.find(item => {
          if (type.includes(item)) {
            return true;
          if (extension.toLowerCase() === item) {
            return true
          } else {
            return false;
            return false
          }
        });
        })
        if (!tempType) {
          this.$message.error(`请上传正确的文件格式!`);
          limitType = false;
          this.$message.error(`请上传正确的文件格式!`)
          limitType = false
        }
      }
      const limit = file.size / 1024 / 1024 < this.fileSizeLimitM;
      const limit = file.size / 1024 / 1024 < this.fileSizeLimitM
      if (!limit) {
        this.$message.error(`上传文件大小不能超过 ${this.fileSizeLimitM}MB!`);
        this.$message.error(`上传文件大小不能超过 ${this.fileSizeLimitM}MB!`)
      }
      return limitType && limit;
      return limitType && limit
    },
  },
  computed: {
    acceptList() {
      let temp = '.*';
    acceptList () {
      let temp = '.*'
      if (this.fileTypeList[this.fileType]) {
        temp = this.fileTypeList[this.fileType].map(item => '.' + item).join(',');
        temp = this.fileTypeList[this.fileType].map(item => '.' + item).join(',')
      }
      return temp;
      return temp
    }
  }
};
}
</script>
<style scoped>