小程序图片上传

// 调用摄像头

 <button class='photo_left_text' bindtap='addphoto'>添加照片</button>
	data:{
		 album: ["album","camera"],//相册或者摄像头
	}
	//选择照片
  addphoto(e){
    let that = this;
    wx.showActionSheet({//选择相册或者拍照api
      itemList: ['相册选择', '拍照'],
      success: (res) => {
        if (res.tapIndex == 0) {
          // 相册
          wx.showLoading({
            title: '上传中',
          });
          wx.chooseImage({//获取图片
            count: "1",//获取图片数量最多能从相册获取9张
            sizeType: ["original"],
            sourceType: [that.data.album[res.tapIndex]],
            success: function (res) {
              that.phonts(res.tempFilePaths[0]);
            },
          })
        } else {
          // 相机
          wx.showLoading({
            title: '上传中',
          });
          wx.chooseImage({
            count: "1",
            sizeType: ["original"],
            sourceType: [that.data.album[res.tapIndex]],
            success: function (res) {
              that.phonts(res.tempFilePaths[0]);
            },
          })
        }

      }
    })
  },
  //调用后台接口把图片上传到后台并返回路径
  phonts(t){
    let that=this;
    wx.uploadFile({
      url: 'https://test.com',//后台接口
      filePath: t,
      name: 'file',//后台接口的名字
      success(r) {
        console.log('成功', r)
        wx.hideLoading();
        that.setData({
          photo: r.data
        });
      },
      fail(e) {
        console.log("失败", e)
      }
    })
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值