element-ui 实现文件上传(多种文件格式)
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
<template>
<div>
<el-form :model="dataForm" ref="dataForm" label-width="100px" label-position="left" :rules="rules">
<el-form-item label="文件:" prop='fileList'>
<el-upload class="upload-demo"
ref="upload"
drag
:data="{ type: 'other' }"
:limit="5"
accept=" .xlsx, .pdf, .doc, .docx, .xls, .rar, .zip, .png , .jpg, .jepg"
:action="url"
:headers="headers"
:file-list="dataForm.fileList"
:auto-upload="true"
:before-upload="beforeUpload"
:on-remove="handleRemove"
:on-exceed="handleExceed"
:on-change="handleChange"
:on-success="handlePreview">
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">支持上传格式:pdf、doc、docx 、xlsx 、xls、 ppt 、pptx 、zip 、rar 、png 、jpg
、jpeg一次最多上传5个文件
</div>
</el-upload>
</el-form-item>
</el-form>
</div>
</template>
// 上传前校验
beforeUpload(file) {
if(file.size>1024*1024*500){
this.$message.warning("你上传的 \“"+file.name+"\” 文件过大,请上传小于500M的文件。");
return false;
}
if(file.name.indexOf('@')!=-1 ||file.name.indexOf('#')!=-1||file.name.indexOf('""')!=-1||file.name.indexOf('<')!=-1||file.name.indexOf('>')!=-1||file.name.indexOf('?')!=-1||file.name.indexOf('|')!=-1||file.name.indexOf('/')!=-1||file.name.indexOf(':')!=-1||file.name.indexOf('`\`')!=-1){
this.$message.warning("你上传的 \“"+file.name+"\” 文件有特殊字符,文件名中不可存在特殊字符,请重新上传");
return false;
}
let index = file.name.lastIndexOf(".");
let Type = file.name.substr(index + 1, file.name.length);
if (['pdf', 'doc', 'docx', 'xlsx', 'xls', 'ppt', 'pptx', 'zip', 'rar', 'png', 'jpg', 'jpeg'].indexOf(Type.toLowerCase()) === -1) {
this.$message({
type: 'warning',
message: '请上传后缀名为pdf、doc、docx 、xlsx 、xls、 ppt 、 pptx 、zip 、rar 、png 、jpg 、jpeg的附件!'
});
return false;
}
},
handleRemove(file, fileList) {
this.dataForm.fileList = fileList
},
handleExceed(files, fileList) {
this.$message.warning(`文件上传数量已达上限`);
},
// 消除文件校验
handleChange(file, fileList) {
if (fileList.length > 0) {
this.dataForm.fileList = fileList
this.$refs.dataForm.clearValidate('fileList') //清除文字校验
}
},
handlePreview(res, file, fileList) {
if (res.code == 0) {
this.dataForm.url = res.data.url
this.dataForm.name = res.data.name
this.dataForm.fileId = res.data.fid
this.dataForm.size = file.size
}
},
export default {
data() {
return {
url: window.SITE_CONFIG["singleUploadURL"],
headers: {Authorization: Cookies.get("access_token")},
}
},
},
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:2 个月前 )
c345bb45
6 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 7 个月前
更多推荐
已为社区贡献4条内容
所有评论(0)