vue中如何上传文件
vue中如何上传文件1、最终实现的效果图2、主要用到的是element-ui中图片上传和进度条组件,下面附上具体实现代码<template><div class="admin-tenant-detail"><el-dialogtitle="导入":visible="visible"custom-class="detail-dialog":before-close="h
·
vue中如何上传文件
1、最终实现的效果图
2、主要用到的是element-ui中图片上传和进度条组件,下面附上具体实现代码
<template>
<div class="admin-tenant-detail">
<el-dialog
title="导入"
:visible="visible"
custom-class="detail-dialog"
:before-close="handleClose"
>
<div>
<el-upload
drag
action
:on-change="handleChange"//当改变的时候,触发的事件
:show-file-list="false"//是否显示所传递的文件列表
:http-request="handleUpload"//覆盖默认的上传行为
>
<el-button size="mini" class="import-button">
<img src="../images/daoru.png" class="daochu-icon" />导入
</el-button>
<div class="el-upload__text">点击或将文件拖到这里上传</div>
</el-upload>
<div class="progress-eara" v-if="progressFile.length > 0">
<span class="file-name">文件:</span>
<div class="progress">
<div class="tip-text">
<img src="../images/lianjie.png" />
<span>{{ fileProgress.fileName }}</span
><i class="el-icon-error" @click="removeFile"></i>
</div>
<el-progress
:stroke-width="4"//进度条的宽度
:percentage="fileProgress.percentage"//进度条所占百分比
:status="fileProgress.status"//进度条的状态
:color="customColor"//颜色
:show-text="false"
></el-progress>
</div>
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="handleClose" size="mini">取 消</el-button>
<el-button
type="primary"
@click="handleSubmit"
size="mini"
v-loading="loading"
>确 定</el-button
>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
props: {
visible: {
type: Boolean,
default: () => {
return false;
},
required: true
}
},
data() {
return {
customColor: '#1E6FFF',
progressFile: [],
fileProgress: {
percentage: 0,
status: '',
fileName: ''
},
file: '',
loading: false
};
},
methods: {
handleClose() {
this.$emit('update:visible', false);
this.removeFile();
},
handleChange(file) {
this.file = file.raw;
},
handleUpload() {
const { name, size } = this.file;
const nameType = name.split('.');
const fileType = ['xlx', 'xlsx'];
const nameTypeIndex = fileType.findIndex(
item => item === nameType[nameType.length - 1]
);
const limitSize = size / 1024 / 1024 <= 10;
if (nameType.length > 0 && nameTypeIndex === -1) {
this.$message({
type: 'error',
message: '上传文件格式有误,请上传{.xlx,.xlsx}类型的文件'
});
return;
}
if (!limitSize) {
this.$message({
type: 'error',
message: '上传的文件不可大于10M,请重新选择'
});
return;
}
this.fileProgress.percentage = 100;
this.fileProgress.status = 'success';
this.fileProgress.fileName = name;
this.progressFile = [this.file];
},
handleSubmit() {
this.loading = true;
let params = new FormData();
params.append('file', this.progressFile[0]);
if (!this.progressFile[0]) {
this.$message.error('请上传文件');
this.loading = false;
return;
}
this.$api.heartbeat
.importFile(params)
.then(res => {
this.loading = false;
if (res.code === this.$api.code.SUCCESS) {
this.$message({
type: 'success',
message: '导入数据成功'
});
this.handleClose();
this.$emit('updateHeartbeatList')
}
})
.catch(() => {
this.loading = false;
});
},
removeFile() {
this.progressFile = [];
}
}
};
</script>
<style scoped lang="scss">
.daochu-icon {
width: 12px;
height: 12px;
margin-right: 5px;
}
.import-button {
border: 1px solid #1e6fff;
color: #1e6fff;
margin-top: 60px;
margin-bottom: 10px;
}
.progress-eara {
margin: 24px 60px 0px;
display: flex;
.file-name {
margin-right: 8px;
height: 22px;
font-size: 16px;
font-weight: 500;
color: #242628;
line-height: 22px;
}
.progress {
flex: 1;
.el-icon-error {
font-size: 14px;
color: #1e6fff;
margin-left: 16px;
}
}
.tip-text {
display: flex;
align-items: center;
height: 18px;
font-size: 12px;
font-family: PingFangSC-Regular, PingFang SC;
color: #56585a;
line-height: 18px;
text-align: left;
margin-bottom: 2px;
}
}
</style>
<style lang="scss">
.admin-tenant-detail {
.detail-dialog {
width: 680px;
}
}
</style>
更多推荐
所有评论(0)