base64apph5_0">uniapp将图片url转换成base64支持app和h5
imageToBase64支持app和h5,
app内使用plus.io.resolveLocalFileSystemURL
方法转换
h5内使用uni.request
方法转换
// 图片转base64
export const imageToBase64 = (path) => {// #ifdef APP-PLUSreturn new Promise((resolve, reject) => {plus.io.resolveLocalFileSystemURL(path, function(entry) {entry.file(function(file) {var fileReader = new plus.io.FileReader();fileReader.readAsDataURL(file);fileReader.onloadend = function(evt) {resolve(evt.target.result);}})})})// #endif// #ifdef H5return new Promise((resolve, reject) => {uni.request({url: path,method: 'GET',responseType: 'arraybuffer',success: res => {let base64 = uni.arrayBufferToBase64(res.data); //把arraybuffer转成base64base64 = 'data:image/jpeg;base64,' + base64resolve(base64)},fail: err => {reject(err)}})})// #endif
}