<input
type="file"
ref="clearFile"
style="display:none"
@change="uploadImg($event)"
class="add-file-right-input"
accept="image/png,image/jpeg,image/gif,image/jpg"
/>
uploadPortrait(flag) {
console.log("=====flag======", flag);
this.uploadFlag = flag;
this.$refs.clearFile.click(); //上传文件
},
uploadImg(e) {
console.log("====ee====", e);
let file = e.target.files || e.dataTransfer.files; //target事件
// let docObj = document.getElementById("file");
console.log("=======file=====", file);
// console.log("=======file===111==", docObj.files);
this.uploadBackstage(file);
// this.$refs.clearFile.value = ""; //上传文件后重置
},
uploadBackstage(files) {
let formData = new window.FormData();
formData.append("userId", this.userId);
formData.append("type", 1);
// let mainUrls = "http://120.26.91.101/api/daikin/userPublicInfo/upload";
// let imgPath = "";
for (let i = 0; i < files.length; i++) {
formData.append("multipartFile", files[i]);
// imgPath = this.getUrl(files[i]);
console.log("====getUrl===", files[i].name);
}
upload(formData)
.then(res => {
this.$refs.clearFile.value = "";
console.log("res===", this.uploadFlag);
if (this.uploadFlag === 1) {
this.form.portrait = res.body.url;
} else if (this.uploadFlag === 2) {
this.form.backgroundPortrait = res.body.url;
}
console.log("====this.form.portrait====", this.form.portrait);
})
.catch(() => {
this.$refs.clearFile.value = "";
});
}
export function upload(data) {
return request({
url: "daikin/userPublicInfo/upload",
method: "post",
data: data,
headers: {
"Content-Type": "multipart/form-data"
}
});
}
0 个回复