uniapp 上传

图片上传组件

使用组件实现图片上传功能,以下是相关的代码:

组件声明

fileList1: [],
// uviewui ui组件
<u-upload
    uploadText="上传打款凭证"
    :fileList="fileList1"
    @afterRead="afterRead"
    @delete="deletePic"
    name="1"
    :multiple="false"
    :maxCount="1"
></u-upload>

新增图片方法

afterRead(event) {
    this.imgUrl = '';
    let uploadTask = uni.uploadFile({
        url: this.$imgURL + '/api/upload/imgFile', // 仅为示例,非真实的接口地址
        filePath: event.file.url,
        name: 'file',
        formData: {
            'token': Q.getToken()
        },
        success: (uploadFileRes) => {
            this.imgUrl = JSON.parse(uploadFileRes.data).data;
        }
    });

上传过程处理

    // 上传中
    let lists = [].concat(event.file);
    let fileListLen = this[\`fileList${event.name}\`].length;
    lists.map((item) => {
        this[\`fileList${event.name}\`].push({
            ...item,
            status: 'uploading',
            message: '上传中'
        });
    });
    // 上传完成
    uploadTask.onProgressUpdate((res) => {
        if (res.progress === 100) {
            setTimeout(() => {
                let item = this[\`fileList${event.name}\`][fileListLen];
                this[\`fileList${event.name}\`].splice(fileListLen, 1, Object.assign(item, {
                    status: 'success',
                    message: '',
                    url: event.file.url
                }));
            }, 1000);
        }
        // 测试条件,取消上传任务。
        /*  if (res.progress > 50) {
            uploadTask.abort();
        }*/
    });
},

posted on   完美前端  阅读(601)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2019-09-09 js 判断滚动到底部 自动加载
2019-09-09 JS如何判断滚动条是否滚到底部

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示