天天看点

小程序上传图片 wx.chooseImage wx.uploadFile

wx.chooseImage({
  count: 1, //最多可以选择的图片张数 默认值是9
  sizeType: ['original', 'compressed'], // 所选的图片的尺寸 original原图 compressed压缩图
  sourceType: ['album', 'camera'], // 选择图片的来源 album从相册选图 album使用相机
  success (res) {
    // tempFilePath可以作为img标签的src属性显示图片
    const tempFilePaths = res.tempFilePaths
     wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址 开发者服务器地址
      filePath: tempFilePaths[0], //要上传文件资源的路径 (本地路径)
      name: 'file', //文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容
      formData: {
        'user': 'test'
      }, //HTTP 请求中其他额外的 form data
      success (res){
        const data = res.data
        //do something
      }
    })
  }
})
           

https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html