美文网首页
uniapp 上传图片 单张上传 多张上传,图片加参数上传

uniapp 上传图片 单张上传 多张上传,图片加参数上传

作者: 放逐的帝王 | 来源:发表于2021-03-30 10:00 被阅读0次

    文章会记录 单张上传 两张上传 多张不确定图片上传

    最近做了一个uniapp项目,需要用户上传身份证正反面提交给后台

    上传类型 content-type='multipart/form-data' 参数 file

    接口图片.png

    需要上传两张图片

    直接上代码

            uni.uploadFile({
             url:'/api/jsd-web/webct/info/uploadIdCardImg',     //post请求的地址
              files:[
                  {name:'file1',file:this.IDImgData.zheng_img},
                  {name:'file2',file:this.IDImgData.fan_img}
                   ],
                header:{
                'token':***********,
             },
            success: (res) => {
                // do what you should do...
             }
        })
    

    我的代码


    image.png

    以上是两张上传 (确定的两张) ,可以把 两张获取到 file文件保存下来 提交的时候,一起提交

    单张上传

    uni.uploadFile({
         url:'/api/jsd-web/webct/creditqiuck/faceRecognition',      //post请求的地址
        /* files:[
              {name:'file1',file:that.IDImgData.zheng_img},
          {name:'file2',file:that.IDImgData.fan_img}
         ], */
           file:file,
           name:'file',
           formData:{  //后台所需除图片外的参数可以写在这里面 ,单张多张都可
              '参数名':参数值
           },
        header:{
           'token':token,
         },
           success: (res) => {
          // do what you should do...                               
        }
    })
    
    我的代码.png

    上面的代码 已将 单张 两张 上传分开

    多张上传

    uni.chooseImage({
        success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            let imgs = tempFilePaths.map((value, index) => {
                return {
                name: index,
                uri: value
            })
            uni.uploadFile({
                url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
                files: imgs,
                success: (uploadFileRes) => {
                    console.log(uploadFileRes.data);
                }
            });
        }
    });
    

    上为多张一起上传

    多张单个上传

    uni.chooseImage({
        success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            for(let i = 0;i < tempFilePaths.length; i++) {
                uni.uploadFile({
                    url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
                    filePath: tempFilePaths[i],
                    name: 'file',
                    formData: {
                        'user': 'test'
                    },
                    success: (uploadFileRes) => {
                        console.log(uploadFileRes.data);
                    }
                });
            }
        }
    });
    

    多张上传 看后台需求,最后两段代码应该用哪一个

    相关文章

      网友评论

          本文标题:uniapp 上传图片 单张上传 多张上传,图片加参数上传

          本文链接:https://www.haomeiwen.com/subject/qmjwhltx.html