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
|
methods: {
emitInput(fileList) {
let arr = [];
for (let i = 0; i < fileList.length; i++) {
arr.push(fileList[i].url);
}
this.$emit('input', arr)
},
handleRemove(file, fileList) {
this.emitInput(fileList);
},
handlePreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
beforeUpload(file) {
// Tell if the image is larger than 5M
const isLt5M = file.size / 1024 / 1024 < 5;
// const fileType = file.name.substring(file.name.lastIndexOf('.')+1)
// Identify the type of the image
// const isJpg = file.type == 'image/jpeg' || file.type == 'image/jpg' || file.type == 'image/png' || file.type == 'image/gif'
// if(!isJpg){
// this.$message.error('You can only upload these image file types: jpg, jpeg, png, gif!')
// return false
// }
if (!isLt5M) {
this.$message.error('The size of the uploaded image cannot exceed 5MB!');
return false
}
let _self = this;
return new Promise((resolve, reject) => {
// policy() is a GET request, not POST
policy().then(response => {
_self.dataObj.policy = response.data.policy;
_self.dataObj.signature = response.data.signature;
_self.dataObj.ossaccessKeyId = response.data.accessKeyId;
_self.dataObj.key = response.data.dir + '/${filename}';
_self.dataObj.dir = response.data.dir;
_self.dataObj.host = response.data.host;
resolve(true)
}).catch(err => {
console.log(err)
reject(false)
})
})
},
handleUploadSuccess(response, file, fileArr) {
console.log('Upload successful!')
let url = this.dataObj.host + '/' + this.dataObj.dir + '/' + fileArr.name;
// Get the image url
this.fileList.push({
name: fileArr.name,
url: url
});
this.emitInput(this.fileList);
},
handleExceed(file, fileList) {
this.$message({
message: 'Maximum of' + this.maxCount + 'images can be uploaded',
type: 'warning',
duration: 1000
});
},
}
}
</script>
<style>
</style>
|