ZhangXianQiang
2024-05-17 caf356587e08608be27dc25e7cc63c9b191d9aa5
fix:上传文件类型
1个文件已修改
51 ■■■■ 已修改文件
src/components/UploadC.vue 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/components/UploadC.vue
@@ -1,27 +1,20 @@
<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">
    <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">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传pdf、mp4、mp3、png、jpg、jpge文件,且不超过{{fileSizeLimitM}}M</div>
      <div slot="tip" class="el-upload__tip">只能上传pdf、mp4、mp3、png、jpg、jpge文件,且不超过{{ 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>
</template>
<script>
export default {
  name: "UploadC",
  props: {
@@ -48,24 +41,29 @@
  data() {
    return {
      uploadUrl: "http://localhost:8085/api/upload/upload",
    }
      fileTypeList:{
        'video': ['.mp4', '.avi'],
        'img': ['.jgp', '.png', '.jpeg'],
        'pdf': ['.pdf'],
      }
    };
  },
  methods: {
    clearFile() {
      this.fileUrl = []
      this.fileUrl = [];
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }?`);
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    handleRemove(file, fileList) {
      this.$emit('removeFile', this.fileUrl, file.name);
    },
    handleUploadSuccess(res, file) {
      this.fileUrl.push(res.response)
      this.fileUrl.push(res.response);
      this.$emit('getUploadUrl', this.fileUrl);
    },
    beforeUpload(file) {
      const limit = file.size / 1024 / 1024 < this.fileSizeLimitM;
      if (!limit) {
        this.$message.error(`上传文件大小不能超过 ${this.fileSizeLimitM}MB!`);
@@ -74,18 +72,15 @@
    },
  },
  computed: {
    acceptList: () => {
      let temp = '.jgp,.png,.jpeg'
      if(this.fileType === 'video') {
        temp = '.mp4,.avi'
      }
      if(this.fileType === 'pdf') {
        temp = '.pdf'
    acceptList() {
      let temp = '.*';
      if (this.fileTypeList[this.fileType]) {
        temp = this.fileTypeList[this.fileType].join(',');
      }
      return temp;
    }
  }
}
};
</script>
<style scoped>
@@ -93,13 +88,16 @@
  width: 300px;
  height: 200px;
}
.avatar-uploader {
  text-align: center;
  width: 100%
}
.avatar-uploader-icon:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
@@ -109,6 +107,7 @@
  cursor: pointer;
}
.avatar {
  display: block;
}