Wechat applet upload avatar

Record the function of uploading and modifying the avatar in the process of making WeChat applets. The interface needs to transmit base64 pictures. Use wx.getFileSystemManager().readFile() to convert the format

code:

 wx.chooseMedia({
    
    
     count: 1,
     mediaType: ['image'],
     success: res => {
    
    
         wx.getFileSystemManager().readFile({
    
    
             filePath: res.tempFiles[0].tempFilePath, //选择图片返回的相对路径
             encoding: 'base64', //编码格式
             success: res => {
    
     //成功的回调
                 let url = 'data:image/png;base64,' + res.data
                 $Api.updateHeadImg({
    
    headImg:url}).then(resp=>{
    
    
                     if(resp.statu == 200){
    
    
                         this.setData({
    
    
                             avatar: help.formatImg(resp.data)
                         })
                     }
                 })
             }
           })
         }
 })

Guess you like

Origin blog.csdn.net/qq_44415875/article/details/129947436