xiangpei
2024-05-16 fc902d4ba26ebebf2de18ead235aaafa37f44fcc
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
<template>
  <div>
    <el-upload
        :action="uploadUrl"
        :show-file-list="true"
        :limit="uploadNum"
        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 v-if="fileUrl && fileUrl.length > 0 && uploadNum === 1">
        <video 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"/>
      </div>
    </el-upload>
  </div>
</template>
 
<script>
export default {
  name: "UploadC",
  props: {
    uploadNum: {
      required: false,
      default: 1,
      type: Number
    },
    fileType: {
      required: false,
      type: String
    },
    fileSizeLimitM: {
      required: false,
      default: 1,
      type: Number
    },
    fileUrl: {
      required: true,
      default: [],
      type: Array
    }
  },
  data() {
    return {
      uploadUrl: "http://localhost:8085/api/upload/upload",
    }
  },
  methods: {
    clearFile() {
      this.fileUrl = []
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }?`);
    },
    handleRemove(file, fileList) {
      this.$emit('removeFile', this.fileUrl, file.name);
    },
    handleUploadSuccess(res, file) {
      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!`);
      }
      return limit;
    },
  }
}
</script>
 
<style scoped>
.returnShow {
  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;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
 
}
.avatar {
  display: block;
}
</style>