customUpload.vue
2.11 KB
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-input
clearable
v-model.trim="uploadImgUrl"
size="mini"
@change="changeInput"
>
<template slot="append">
<i class="iconfont iconfolder-o"></i>
<input type="file" class="file" ref="files" @change="getImages" />
</template>
</el-input>
</div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
export default {
model: {
prop: "value",
event: "input"
},
props: {
value: {
type: "",
default: ""
}
},
data() {
return {
requestUrl: process.env.BASE_API + "/file/upload",
headers: {
Authorization: getToken()
},
fileList: [],
uploadImgUrl: ""
};
},
created() {
this.uploadImgUrl = this.value;
},
methods: {
getImages(el) {
let file = el.target.files[0];
let type = file.type.split("/")[0];
if (type === "image") {
this.upload(file);
} else {
this.$message.warn("只能上传图片格式");
}
},
upload(imgUrl) {
let that = this;
console.log(that.headers);
let formdata = new FormData();
formdata.append("file", imgUrl);
axios
.post(this.requestUrl, formdata, {
headers: that.headers
})
.then(response => {
let res = response.data;
if (res.code == "200") {
that.uploadImgUrl = res.data.urlPath;
that.$emit("input", that.uploadImgUrl);
that.$emit("change", that.uploadImgUrl);
}
});
},
changeInput(e) {
if (e) {
this.uploadImgUrl = e;
} else {
this.$refs.files.value = "";
this.uploadImgUrl = "";
}
this.$emit("input", this.uploadImgUrl);
this.$emit("change", this.uploadImgUrl);
}
}
};
</script>
<style lang="scss" scoped>
.file {
position: absolute;
width: 100%;
padding: 100%;
right: 0;
top: 0;
opacity: 0;
}
:deep(el-input-group__append),
:deep(el-input-group__prepend) {
padding: 0 10px !important;
overflow: hidden;
}
.iconfont {
font-size: 12px;
}
</style>