coodsImgs: function() {
var _this = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['camera', 'album'],
success(res) {
// tempFilePath可以作为img标签的src属性显示图片
console.log(res.tempFilePaths);
wx.uploadFile({
url: '......' + '/fs/uploadFileToGridFS.json',为测试地址
filePath: res.tempFilePaths[0],
name: 'fileList',
success: function (res) {
var resData = JSON.parse(res.data);
console.log('111图片ID', resData.fileList[0])
console.log(app.globalData.img_prefix_http + resData.fileList[0]);
wx.request({
url: app.globalData.img_prefix_http + resData.fileList[0],
method: 'GET',
responseType: 'arraybuffer',
success: function (res) {
console.log(555, res);
// var base64 = 'data:image/jpg;base64,' + wx.arrayBufferToBase64(res.data);
var base64 = wx.arrayBufferToBase64(res.data);
// console.log(base64);
}
});
}
})
}
})
}
小程序上传图片后 解析为base64 编码格式 只能使用wx.request 并且(responseType: 'arraybuffer’) 才能够获得图片真实路径解析(真机中 小程序图片的临时路径为wxfile:// 前缀, 必须使用 http://前缀才能发送ajax), 所以需先上传服务器, 后得到图片服务器地址 进而解析