elementUI el-upload上传组件实战使用

简介: elementUI el-upload上传组件实战使用
<!-- 导入---------------------------------------- -->
<el-upload
    :show-file-list="false" 
    :headers="headers"
    :action="actionUrl" 
    :before-upload="beforeUpload" 
    :on-success="uploadSuccess"
    :on-error="uploadError"
    :disabled="percent < 100"
    >
        <el-button 
            type="success" 
            icon="el-icon-upload" 
            :loading="percent < 100">
            {{ 100 > percent ? percent
                + "%" : "上传数据" }}
        </el-button>
</el-upload>
<!-- ---------------------------------------- -->
属性_________________________________________________________
// 上传----------------------------------------
headers: {
    token: localStorage.token, //获取token(注意仔细看后端接受token的字段名是不是叫做“token”)
},
actionUrl: `${this.$d.API_ROOT_URL}/customer/importCustomerData`,
fmt: ["xls", "xlsx"],
dur: 100,
percent: 100,
// ----------------------------------------
方法_________________________________________________________
// 上传文件----------------------------------------------------------------
showFakeLoading() {
    this.interval && clearInterval(this.interval);
    this.percent = 0;
    this.interval = setInterval(() => {
        this.percent++;
        this.percent >= 99 && clearInterval(this.interval);
    }, this.dur);
},
hideFakeLoading() {
    this.interval && clearInterval(this.interval);
    this.percent = 100;
},
//文件上传之前
beforeUpload(file, id) {
    // 判断是不是特定的格式________________________
    let isFile = this.fmt.includes(file.name.toLocaleLowerCase().split(".").pop());
    const maxSize = 50; //限制大小
    const isAllowSize = file.size / 1024 / 1024 <= maxSize;
    isFile || this.$message.error("上传文件只能是" + this.fmt + "格式");
    isAllowSize || this.$message.error("上传文件大小不能超过" + maxSize + "MB");
    let allowUpload = isFile && isAllowSize;
    allowUpload && this.showFakeLoading(); //虚假加载
    return allowUpload; //若返回false则停止上传
},
//上传成功
uploadSuccess(response, file, fileList) {
    this.hideFakeLoading();
    if (response.data && response.data.key) {
        // 下载失败原因的描述文件
        this.$d.customer_downloadImportCustomerExcel({ key: response.data.key }, {
            s: (d) => {
                this.$g.downloadFile(d, `“${file.name}”上传失败原因`, '.xls');
                this.$message.error(`“${file.name}”上传失败,请查看失败原因`);
                this.initList(true);//可能上传导入文件部分成功了,所以也需要刷新列表
                console.log('上传失败', response, file, fileList);
            }
        });
    } else if (response.success) {
        // 上传成功了
        this.$message.success(`“${file.name}”导入成功`);
        this.initList(true);//刷新列表
        console.log('上传成功', response, file, fileList);
    } else {
        // 其他失败原因
        this.$message.error(response.msg);
        console.log('上传失败', response, file, fileList);
    }
},
//上传失败
uploadError(err, file, fileList) {
    this.hideFakeLoading();
    this.$message.error("上传失败");
    console.log('上传失败', err, file, fileList);
},
// ----------------------------------------
样式_________________________________________________________
// 上传按钮禁用状态----------------------------------------
>>>.el-upload {
    cursor: not-allowed;
}

相关扩展阅读

image.png

相关文章
|
移动开发 JavaScript 前端开发
如何处理 h5 使用 iframe 嵌套页面,内外 viewport 不一致导致的缩放问题?
如何处理 h5 使用 iframe 嵌套页面,内外 viewport 不一致导致的缩放问题?
2565 0
|
JavaScript 算法 Linux
【vue报错】error:0308010C:digital envelope routines::unsupported
【vue报错】error:0308010C:digital envelope routines::unsupported
748 3
|
12月前
|
搜索推荐 数据管理 数据处理
基于 Axure 与 Element UI 风格的拖动列调整位置表格设计
本设计基于Axure实现Element UI风格的可拖动列排序表格,提升数据展示灵活性。通过动态面板与交互事件,支持用户拖拽调整列顺序,并实时更新布局,配合视觉反馈与规范样式,增强操作体验,适用于多种数据管理场景。
527 5
|
存储 Kubernetes 数据安全/隐私保护
k8s--配置存储 ConfigMap、Secret
k8s--配置存储 ConfigMap、Secret
|
应用服务中间件
Tomcat请求特殊字符参数问题:Invalid character found in the request target. The valid characters are defined in
Tomcat请求特殊字符参数问题:Invalid character found in the request target. The valid characters are defined in
2010 0
Tomcat请求特殊字符参数问题:Invalid character found in the request target. The valid characters are defined in
|
JavaScript 算法 安全
出现错误,无法启动 Visual Studio Exit Code:9
出现错误,无法启动 Visual Studio Exit Code:9
957 2
出现错误,无法启动 Visual Studio Exit Code:9
|
JavaScript 前端开发
vue全局公共组件自动引入并注册,开发效率直接起飞!
【10月更文挑战第14天】vue全局公共组件自动引入并注册,开发效率直接起飞!
792 1
|
JavaScript 前端开发 UED
​基于 vue + element plus + node 实现大文件分片上传,断点续传和秒传的功能!牛哇~
​基于 vue + element plus + node 实现大文件分片上传,断点续传和秒传的功能!牛哇~
1140 11
|
数据挖掘 索引 Python
python数据分析之pandas超详细学习笔记(下)
简介:pandas,python+data+analysis的组合缩写,是python中基于numpy和matplotlib的第三方数据分析库,与后两者共同构成了python数据分析的基础工具包,享有数分三剑客之名。
563 149
element组件库笔记一:element 框架中table表格复选框选中后,切换下一页之前选中复选框和数据消失的问题
这篇文章介绍了在Element UI框架中,如何解决表格组件复选框在分页时选中状态丢失的问题。
1568 0