-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.vue
41 lines (39 loc) · 1 KB
/
index.vue
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
<template>
<input v-show="false" ref="inputFile" type="file" @change="inputChange" />
</template>
<script>
import { upload } from '@/api/upload';
export default {
name: 'Upload',
props: {
maxSize: {
type: Number,
default: 20,
required: false,
},
},
methods: {
uploadStart() {
this.$refs.inputFile.click();
},
inputChange(event) {
const input = event.target;
const files = event.target.files;
if (files && files[0]) {
if (files[0].size > this.maxSize * 1024 * 1024) {
this.$baseNotify(`文件的容量不得超过${this.maxSize}Mb`, '上传失败', 'error');
return;
}
this.upload(files[0]);
}
input.value = '';
},
async upload(blob) {
const formData = new FormData();
formData.append('file', blob);
const { data } = await upload(formData);
this.$emit('success', data);
},
},
};
</script>