今日用uni-app实现上传图片功能,但后台需转64格式,研究了大半天,终于实现了
uni.chooseImage({
count: 6, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function (res) {
this.urlTobase64(res.tempFilePaths[0])
}
});
urlTobase64(url){
uni.request({
url: url,
method:'GET',
responseType: 'arraybuffer',
success: ress => {
let base64 = wx.arrayBufferToBase64(ress.data); //把arraybuffer转成base64
base64 = 'data:image/jpeg;base64,' + base64 //不加上这串字符,在页面无法显示的哦
console.log(base64)
}
})
}
方法二:
uni.getFileSystemManager().readFile({
filePath: item, //选择图片返回的相对路径
encoding: 'base64', //编码格式
success: res => { //成功的回调
let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符,在页面无法显示的哦
that.dataList.picList.push(base64);
}
})
仅支持微信小程序