今日用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);
}
})
僅支持微信小程序