小程序调用相机拍照上传

用的wpy框架,有this,原声小程序就按照你们的调方法就行了

     //打开相机
    openCream() {
        const _this = this;
        wx.showActionSheet({
          itemList: ["拍照"],//['拍照','相册']
          itemColor: "",
          //成功时回调
          success: function(res) {
            if (!res.cancel) {
              /* res.tapIndex返回用户点击的按钮序号,从上到下的顺序,从0开始比如用户点击本例中的拍照就返回0,相册就返回1我们res.tapIndex的值传给chooseImage()*/
              _this.chooseImage(res.tapIndex);
            }
          },
          //失败时回调
          fail: function(res) {
            console.log("调用失败");
          },
          complete: function(res) {},
        });
      },
    chooseImage(){
        let this_ = this;
        wx.chooseImage({
          //count表示一次可以选择多少照片
          count: 1,
          //sizeType所选的图片的尺寸,original原图,compressed压缩图
          sizeType: ["original", "compressed"],
          //如果sourceType为camera则调用摄像头,为album时调用相册
          sourceType: ["camera"],
          success(res) {
            // tempFilePath可以作为img标签的src属性显示图片
            const tempFilePaths = res.tempFilePaths;
            this_.temSrc = tempFilePaths[0];//保存选择图片的本地地址,需要上传得到真正的url
            wx.uploadFile({//上传
              url: api.fileUpload,
              filePath: tempFilePaths[0],
              header: {
                uapAccessToken: accessToken,
                uapRefreshToken: refreshToken,
            },
            name: 'file',
               success(re) {
                 const data = JSON.parse(re.data);
                 this_.src = data.url
                 this_.$apply()//wpy框架需要
              },
              fail() {
                 console.log()
              }
            })
       },
    });
  }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值