小程序 上传图片 相册 拍照

 wx.chooseImage({
      count: 1, // 张数, 默认9
      sizeType: ['compressed'], // 建议压缩图
      sourceType: ['album'], // 来源是相册  camera:拍照
      success: (res) => {
        if (res.tempFiles[0] && res.tempFiles[0].size > 5242880) {
          wx.showToast({
            title: '请上传小于5M的图片',
            icon: 'none',
          });
          return false;
        }
        this.uploadImage(res.tempFilePaths[0]);
      },
});

 

uploadImage(tempavatarUrl) {
    wx.uploadFile({
      url: url,//链接地址
      filePath: tempavatarUrl,
      name: 'file',
      header: {
        'Content-Type': 'multipart/form-data',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
      },
      success: (res) => {
        const data = JSON.parse(res.data);
        if (data.success===true) {
          console.info('success')
        } else {
          wx.showToast({
            title: '上传头像失败',
            icon: 'none',
          });
        }
      },
    });
  },

你可能感兴趣的:(小程序)