<el-upload
ref="myupload"
class="upload-demo"
drag
:fileList="fileList"
:action="actionPath"
:headers="headers"
:on-error="fileError"
:on-progress="onProgress"
:before-upload="beforeUpload"
:http-request="toUpload"
:show-file-list="false"
accept=".xls,.xlsx"
:on-change="onChange"
:auto-upload="false"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">拖拽文件至此处或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip" style="margin-bottom: 12px">
支持xls、xlsx表格的excel表格,大小不超过100MB
</div>
</el-upload>
通过http-request 实现
:http-request="toUpload"
onChange 判断文件 并获取需要展示的数据
onChange(file) {
if (this.allowType) {
const isLt2M = file.size / 1024 / 1024 > 100
if (isLt2M) {
this.$message.warning('文件大小不能超过 100MB!')
this.uploadLoading = false
return false
}
var index = this.allowType.indexOf(file.name.substring(file.name.lastIndexOf('.')).toLowerCase())
if (index < 0) {
this.$message({
message: '文件格式不正确',
type: 'warning'
})
this.uploadLoading = false
return false
}
}
this.fileList = [file]
this.fileInfoParams = [
{
name: file.name,
size: file.size
}
]
},
手动点击上传
clickConfirmImport(){
this.$refs.myupload.submit()
}
toUpload(param) {
console.log(param)
const formData = new FormData()
formData.append('file', param.file)
axios({
url: param.action,
method: 'post',
data: formData,
headers: { 'X-Token': getToken() }
}).then((res) => {
console.log(res)
})
},