微信小程序chooseImage,上传图片到服务器,真机调试不能调式的解决办法

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), 所以需先上传服务器, 后得到图片服务器地址 进而解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值