小程序 上傳圖片 相冊 拍照

 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',
          });
        }
      },
    });
  },

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章