微信小程序上传图片

wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: (res) => {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths[0];//拍照和选取本地图片都是数组
        wx.uploadFile({
          url: 'https://uploads', //仅为示例,非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: (res) => {
            if (res.statusCode == 200 && res.errMsg == "uploadFile:ok"){
              let data = JSON.parse(res.data);
              let tempImg = data[0].url;
              let tem = 'person.avatar'
              this.setData({
                [tem]: tempImg
              })
            }
          },
          fail: function (res) {
          }
        })
      },
      fail: function (res) {
      }
    })

 

posted @ 2019-05-31 15:46  温如其玉  阅读(363)  评论(0编辑  收藏  举报