微信小程序上传图片转化为base64格式

上传图片函数

 clickimage: function(e) {
    
    
    var index = e.currentTarget.dataset.index;
    var count = 4;
    var that = this;
    wx.chooseImage({
    
    
      count: count,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
    
    
        var tempFilePaths = res.tempFilePaths;
        for (var x = 0; x < res.tempFilePaths.length; x++) {
    
    
          wx.getFileSystemManager().readFile({
    
    
            filePath: res.tempFilePaths[x], //选择图片返回的相对路径
            encoding: "base64",//这个是很重要的
            success: res => {
    
     //成功的回调
             //返回base64格式
             console.log('data:image/png;base64,' + res.data)
            }
          })
        }
      }
    })
  },

在小程序中查看在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42981560/article/details/112992628