element-ui中的el-upload 点击上传
<template>
<div class="app-container">
<el-button type="primary" icon="el-icon-plus" size="mini" @click="Addfile()">上传</el-button>
</div>
<el-dialog
title="上传文件"
:visible.sync="dialogVisiblefile"
width="30%"
:before-close="handleClosefile">
<el-upload
ref="upload"
:accept="fileType.join(',')"
:limit="1"
:headers="upload.headers"
:action="upload.url"
:before-upload="beforeUpload"
:disabled="upload.isUploading"
:on-progress="handleFileProgress"
:on-success="handleFileSuccess"
:auto-upload="false"
drag
>
<i class="el-icon-upload" />
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div slot="tip" class="el-upload__tip" style="color: red">
提示:仅允许导入“xls”或“xlsx”格式文件!
</div>
</el-upload>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisiblefile = false">取 消</el-button>
<el-button type="primary" @click="submitUpload()">确 定</el-button>
</span>
</el-dialog>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
data() {
return {
dialogVisiblefile: false,
fileType: ['.xlsx', '.xls'],
upload: {
// 设置上传的请求头部
headers: { Authorization: getToken() },
// 上传地址
url: process.env.VUE_APP_BASE_API + '/common/upload',
// 是否更新已经存在的用户数据
isUploading: false
},
}
}
methods: {
Addfile(){
this.dialogVisiblefile = true
},
// 上传文件限制
beforeUpload(file) {
const isExcel = /\.(xls|xlsx)$/.test(file.name);
if (!isExcel) {
this.$message.error('上传文件只能是 .xls、.xlsx 格式!');
return false;
}
return true;
},
// 文件上传中处理
handleFileProgress() {
this.upload.isUploading = true
},
// 文件上传成功处理
handleFileSuccess() {
this.upload.isUploading = false
this.$refs.upload.clearFiles()
},
// 提交上传文件
submitUpload() {
this.$refs.upload.submit()
this.dialogVisiblefile = false
},
}
}
</script>