elementUI上传文件应用前后端联调
<template>
<!-- 上传文件弹窗 -->
<div class="openDialog">
<h1>打开Dialog</h1>
<el-button type="primary" @click=";(openDialog = true), changeTitle('结束')" class="end">结束</el-button>
<el-button type="primary" @click=";(openDialog = true), changeTitle('同意')" class="agree">同意</el-button>
<el-button type="primary" @click=";(openDialog = true), changeTitle('驳回')" class="reject">驳回</el-button>
<el-dialog :title="choiceTitle" :visible.sync="openDialog">
<div class="opinions">
<span>审批意见</span>
<div>
<el-input type="textarea" v-model="textarea"></el-input>
</div>
</div>
<div class="upload">
<el-upload
class="uploadDemo"
ref="upload"
action="http://10.10.120.211:30999/form/center/file/upload"
:on-remove="handleRemove"
:before-upload="handleBefore"
:before-remove="beforeRemove"
:on-exceed="handleExceed"
:file-list="uploadFiles"
:limit="5"
:show-file-list="true"
:auto-upload="true"
multiple
><el-button icon="zrx-icon iconicon_24_common_upload">上传文件</el-button>
<div slot="tip" class="el-upload__tip">支持扩展名:rar .zip .doc .docx...(最多可上传5个,每个不超过20M)</div>
</el-upload>
</div>
<div class="foot">
<el-button type="primary" @click=";(openDialog = false), uploadTheFile()" :disabled="this.uploadFiles.length == 0 ? true : false">确定</el-button>
<el-button @click="openDialog = false">取消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
openDialog: false,
textarea: '',
choiceTitle: '',
uploadFiles: [],
agreeUploadFile: [],
deletFileList: []
}
},
methods: {
// 上传前
handleBefore(file) {
this.uploadFiles.push(file)
const uploadSize = Number(file.size / 1024 / 1024)
if (uploadSize > 20) {
alert('文件大小超过20M')
return false
}
return false
},
// 移除前
beforeRemove(file, filelist) {},
// 移除时
handleRemove(file, filelist) {
console.log(file.percentage)
this.deletFileList = filelist
if (file.percentage === undefined) {
for (const key in this.uploadFiles) {
if (file.name == this.uploadFiles[key].name) {
this.uploadFiles.splice(key, 1)
}
}
}
console.log('uploadFiles:', this.uploadFiles)
},
handleExceed(files) {
this.$message.warning(`当前限制选择 5 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + this.uploadFiles.length} 个文件`)
},
changeTitle(val) {
this.choiceTitle = val
},
async uploadTheFile() {
const attachment = []
const uploadAllFile = {}
for (const key in this.uploadFiles) {
this.uploadAllFile = this.uploadFiles[key]
await this.$postMethod(this.$apikeyMap.todo.uploadFile, { payload: { file: this.uploadAllFile }, formData: true }, { baseURL: 'http://10.10.120.211:30999' }).then(
res => {
console.log(this.uploadFiles[key])
this.agreeUploadFile.push({ name: res.data.data.fileOriginalName, type: res.data.data.fileSuffix, url: res.data.data.link, description: '暂无' })
}
)
}
this.attachment = {
attachments: this.agreeUploadFile,
comment: this.textarea
}
this.$putMethod(this.$apikeyMap.todo.agreeUpload, { payload: this.attachment }, { baseURL: 'http://10.10.120.211:30999' }).then(res => {})
}
}
}
</script>
<style lang="scss" scoped>
.openDialog ::v-deep {
.el-button {
border-radius: 2px;
&.is-disabled {
opacity: 0.3;
color: #3b4155;
background: #f4f5f8;
border: 1px solid #dcdfe8;
}
}
.end {
background-color: $red;
border-color: $red;
&:hover {
background-color: $red-hover;
border-color: $red-hover;
}
&:focus {
background-color: $red-active;
border-color: $red-active;
}
}
.agree {
background-color: $blue;
border-color: $blue;
&:hover {
background-color: $blue-hover;
border-color: $blue-hover;
}
&:focus {
background-color: $blue-active;
border-color: $blue-active;
}
}
.reject {
background-color: $yellow;
border-color: $yellow;
&:hover {
background-color: $yellow-hover;
border-color: $yellow-hover;
}
&:focus {
background-color: $yellow-active;
border-color: $yellow-active;
}
}
.el-dialog__wrapper {
display: flex;
justify-content: center;
.el-dialog {
position: fixed;
width: 400px;
height: 380px;
border-radius: 2px;
&::-webkit-scrollbar {
width: 12px;
height: 12px;
background-color: $white;
}
&::-webkit-scrollbar-track {
border-radius: 2px;
background-color: transparent;
}
// 滚动条滑块
&::-webkit-scrollbar-thumb {
// height: 4px;
border: 4px solid $white;
border-radius: 10px;
background-color: rgba(59, 65, 85, 0.2);
cursor: pointer;
&:hover {
border: 2px solid $white;
}
&:active {
background-color: rgba(171, 173, 181);
}
}
// 头部区域
.el-dialog__header {
// overflow: hidden;
border-bottom: 1px solid #e0e0e0;
background-color: $white;
width: 400px;
height: 40px;
padding: 10px 8px 10px 16px;
position: fixed;
z-index: 999;
// 标题文字
.el-dialog__title {
font-family: MicrosoftYaHei-Bold;
font-size: 14px;
color: #3b4155;
line-height: 20px;
}
// 头部关闭按钮
.el-dialog__headerbtn {
top: 8px;
right: 8px;
width: 24px;
height: 24px;
background-image: url('../../assets/svg-icon/common_close.svg');
background-size: 100% 100%;
background-position: center center;
background-repeat: no-repeat;
&:hover {
cursor: pointer;
}
.el-dialog__close {
color: rgba(0, 0, 0, 0.7);
font-size: 12px;
&::before {
content: none;
}
}
}
}
.el-dialog__body {
padding: 0;
margin-top: 40px;
.opinions {
padding: 12px 0;
margin: 0 16px;
span {
margin-bottom: 4px;
display: inline-block;
opacity: 0.7;
font-family: MicrosoftYaHei;
font-size: 14px;
color: $black;
line-height: 24px;
}
.el-textarea {
.el-textarea__inner {
border-radius: 2px;
width: 368px;
height: 92px;
resize: none;
&:hover {
border: 1px solid $blue;
box-shadow: 0 0 4px 2px rgba(66, 99, 254, 0.1);
}
&:focus {
border: 1px solid $blue;
box-shadow: 0 0 4px 2px rgba(66, 99, 254, 0.1);
}
&::-webkit-scrollbar {
width: 12px;
height: 12px;
background-color: $white;
}
&::-webkit-scrollbar-track {
border-radius: 2px;
background-color: transparent;
}
// 滚动条滑块
&::-webkit-scrollbar-thumb {
// height: 4px;
border: 4px solid $white;
border-radius: 10px;
background-color: rgba(59, 65, 85, 0.2);
cursor: pointer;
&:hover {
border: 2px solid $white;
}
&:active {
background-color: rgba(171, 173, 181);
}
}
}
}
}
// 上传区域
.upload {
// margin: 24px 0 0 0;
// 上传按钮
.el-button {
margin: 12px 16px 0 16px;
width: 96px;
padding: 0 8px 0 4px;
border: 1px solid #dadde6;
border-radius: 2px;
&:hover {
background: #f4f5f8;
}
&:active {
background: #e8eaf0;
}
i {
font-size: 24px;
margin-right: 4px;
}
span {
font-family: MicrosoftYaHei;
text-align: center;
font-size: 14px;
color: #3b4155;
line-height: 20px;
font-weight: 400;
}
}
// 列表区域包括按钮
.uploadDemo {
// 提示行文字
.el-upload__tip {
margin: 6px 0 0 16px;
opacity: 0.7;
font-family: MicrosoftYaHei;
font-size: 12px;
color: #3b4155;
}
// 列表ul区域
.el-upload-list {
overflow: auto;
height: 74px;
padding: 0 16px 0 12px; // 单行样式
.el-upload-list__item {
border-bottom: 1px solid#ebebeb;
margin-top: 4px;
&:hover {
background-color: $white;
.el-icon-close {
opacity: 1;
}
}
// 列表删除按钮容器
.el-upload-list__item-status-label {
background-image: url('../../assets/svg-icon/common_close_sm.svg');
background-size: 100% 100%;
background-position: center center;
background-repeat: no-repeat;
.el-icon-upload-success {
&::before {
content: none;
}
}
}
&:first-child {
margin-top: 12px;
}
// 列表行
.el-upload-list__item-name {
display: flex;
align-items: flex-end;
height: 28px;
box-sizing: content-box;
padding: 0;
opacity: 0.9;
font-size: 12px;
color: #3b4155;
.el-icon-document {
opacity: 0.9;
color: #3b4155;
line-height: 28px;
text-align: center;
background-image: url('../../assets/svg-icon/common_attach.svg');
background-size: 100% 100%;
background-position: center center;
background-repeat: no-repeat;
&::before {
content: none;
}
}
}
// 列表关闭按钮外部容器
.el-upload-list__item-status-label {
width: 24px;
height: 24px;
text-align: center;
right: 0;
top: 0;
margin: 2px 0;
}
// 列表关闭按钮图片
.el-icon-close {
width: 24px;
height: 24px;
line-height: 24px;
text-align: center;
font-size: 10px;
top: 0;
right: 0;
margin: 2px 0;
color: rgba(0, 0, 0, 0.7);
background-image: url('../../assets/svg-icon/common_close_sm.svg');
background-size: 100% 100%;
background-position: center center;
background-repeat: no-repeat;
&::before {
content: none;
}
}
// 列表icon容器
.el-upload-list__item-name [class^='el-icon'] {
width: 24px;
height: 24px;
margin: 2px;
}
}
}
}
}
.foot {
width: 400px;
height: 56px;
background: $--pro-background;
display: flex;
justify-content: flex-end;
.el-button {
font-size: 14px;
text-align: center;
line-height: 20px;
border-radius: 2px;
margin: 12px 4px;
font-weight: 400;
&.is-disabled {
opacity: 0.3;
color: #3b4155;
background: #f4f5f8;
border: 1px solid #dcdfe8;
}
}
.el-button--primary {
color: $white;
background-color: $blue;
border-color: $blue;
&:hover {
background-color: $blue-hover;
border-color: $blue-hover;
}
&:focus {
background-color: $blue-active;
border-color: $blue-active;
}
}
.el-button--default {
background: #ffffff;
border: 1px solid #dcdfe8;
&:hover {
background: #f4f5f8;
}
&:focus {
background: #e8eaf0;
}
}
}
}
}
}
}
</style>