前端:uni-file-picker和uni-uploadFile集合使用

在网上找不到比较具体的两个api集合的方法。用原生api实现。简单易实现

dom部分

<uni-section  title="选择查验图片" type="line" class="chosepicture">
                        <view class="example-body">
                            <uni-file-picker
                             limit="9"  
                             title="最多选择9张图片"
                             @select="select" 
                             @delete="deletephoto"
                            ></uni-file-picker>
                        </view>
                    </uni-section>    

limit是限制图片的数量

js部分

 data:{ //这里是把返回的图片url地址做个逗号隔开拼接的参数,为了适应字段接口

imageValue:""

}


        select(e){ //select是选择好图片之后的方法
            console.log(e.tempFilePaths[0]) //会得到一个api提供的临时图片地址
                        uni.uploadFile({
                                    url: 'xxxx/common/upload', //上传图片的接口
                                    filePath:e.tempFilePaths[0],
//一定要用这个参数!它会用formata的形式传递参数
                                    name: 'file',
        header: {   //有些接口需要请求头,如果没有可以去掉,像我现在的接口是必须加上token的。
                                      'Authorization': 'Bearer ' + getToken() //请求头
                      },
                                    success: (uploadFileRes) => {//成功的回调,把返回的图片地址拼接进变量
                                        console.log(uploadFileRes.data);
                                        this.imageValue.push(JSON.parse(uploadFileRes.data).url)
                                    },
                                    fail:(res)=>{ //失败的回调
                                        console.log(res)
                                    }
                                });
                    },

deletephoto(e){ //deletephoto是把已经上传的图片叉掉之后的方法。需要把变量中的图片地址去掉
                let index = this.imageValue.indexOf(e.tempFilePath);
                this.imageValue.splice(index, 1);
        },

顺便附上style部分

.example-body {
        padding: 10px;
        padding-top: 0;
    }

    .custom-image-box {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
    }

    .text {
        font-size: 14px;
        color: #333;
    }
    .chosepicture{
        border-radius: 20rpx;
        width: 90%;
    }

效果图:

(此API是支持拍照和本地图片上传的,样式都是好调整的)

  • 24
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值