vue 之 ele上传文件
程序员文章站
2024-01-03 14:03:46
...
vue 之 ele上传文件
- 一点击选取文件的时候,直接对接后端接口,上传文件,获取返回的数据对象
- 可以删除已上传的文件,也需要对接后端接口进行删除
<template>
<div class="upload_file clear_fix">
<!-- <div>uploadAllFile -- {{ uploadAllFile }}</div> -->
<div class="upload_file_title">{{ title }}</div>
<div class="upload_file_btn">
< !-- 这个可以不需要的 可以删除这两个外层的!
<el-form :model="uploadForm" οnsubmit="return false">
<el-form-item label=""> -->
<el-upload
ref="uploadForm"
action=""
:file-list="fileList"
:on-change="handleFileChange"
:on-remove="removeFile"
:auto-upload="false"
multiple
>
<el-button size="small" type="primary" slot="trigger"
>选取文件</el-button
>
</el-upload>
</el-form-item>
</el-form>
<div class="upload_file_tip">
<div>支持格式:</div>
<div>jpg、png、</div>
<div>docx、xlsx等</div>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
import { baseUrl } from "@/utils/request.js";
import { removeFileGet } from "@/api/creditManagement/singleAdd/singleAdd.js";
export default {
name: "UploadFile",
components: {},
data() {
return {
title: "上传文件:",
fileList: [],
uploadForm: {},
file: {}, // 上传文件
uploadAllFile: [], // 上传的文件的所有数据
};
},
watch: {
uploadAllFile(val) {
this.uploadAllFile = val;
this.$emit("uploadAllFile", this.uploadAllFile);
},
},
methods: {
//上传文件,获取文件流
handleFileChange(file, fileList) {
this.file = file.raw;
// 创建表单对象 用于数据的格式 + 用于添加流文件!
let formData = new FormData();
formData.append("files", this.file);
// 发起请求 对接后端接口 + 需要导出基础的url + Token值!
axios
.post(baseUrl + "/eap/uploadFile", formData, {
headers: {
"Content-Type": "multipart/form-data",
Token: JSON.parse(sessionStorage.getItem("token")),
},
})
.then(({ data }) => {
// console.log("data", data.dat);
if (data.code == 0) {
file.buisid = data.dat[0].buisid; // 给当前文件 添加一个id 可用于删除文件!
this.uploadAllFile.push(data.dat[0]);
this.$message({ type: "success", message: "上传文件成功" });
} else {
this.$message({ type: "success", message: "上传文件失败" });
}
});
},
// 删除文件
removeFile(file) {
// console.log("移除文件", file);
removeFileGet(file.buisid).then(({ data }) => {
if (data.code == 0) {
this.$message({ type: "success", message: "删除文件成功" });
} else {
this.$message({ type: "success", message: data.dat });
}
});
this.uploadAllFile = this.uploadAllFile.filter((item) => {
return item.buisid != file.buisid;
});
},
},
};
</script>
<style lang="scss" scoped>
.upload_file {
position: relative;
top: -10px;
.upload_file_title {
float: left;
text-align: right;
width: 110px;
height: 40px;
line-height: 40px;
}
.upload_file_btn {
position: relative;
float: left;
}
.upload_file_tip {
position: absolute;
top: 35px;
left: 0px;
width: 80px;
height: 49px;
div {
font-size: 12px;
font-family: SourceHanSansCN-Regular, SourceHanSansCN;
font-weight: 400;
color: #b9bcc5;
line-height: 14px;
}
}
}
::v-deep .el-button--small {
padding: 6px 16px !important;
}
::v-deep .el-upload-list {
position: relative;
left: 100px;
top: -34px;
width: 640px;
background: #000;
clear: both;
li {
float: left;
width: 33% !important;
}
.el-upload-list__item {
margin-top: 10px !important;
}
}
</style>