注:pc端是指从微信里左下角的小程序面板进入的小程序
首先微信小程序上传图片方法都是 这里后台是用了oss
wx.chooseImage({
count: that.data.count - that.data.detailPics.length, // 最多可以选择的图片张数,默认9
sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
success: function (res) {
let imgs = res.tempFilePaths;
for (var i = 0; i < imgs.length; i++) {
pics.push(imgs[i])
}
that.uploadimg({
url: that.data.uploadUrl, //这里是你图片上传的接口
path: pics, //这里是选取的图片的地址数组
});
},
})
uploadimg: function (data) {
let that = this,
wx.uploadFile({
url: data.url,
filePath: data.path[0],
name: 'file',
formData: {
scene: 'images'
},
// header: {
// // "content-Type": "multipart/form-data",
// "App": "1",
// "Authorization": wx.getStorageSync('tokens')
// },
success: (resp) => {
//注意这里返回的resp是一个字符串
let json_data = JSON.parse(resp.data)
if (json_data.code == 200) {
success++;
let pic_name = json_data.data.url
} else if (json_data.code == 400) {
wx.showToast({
title: '上传失败',
icon: none
})
fail++;
}
},
fail: (res) => {
wx.hideLoading();
wx.showToast({
title: '上传失败',
icon: none
})
},
});
},
以上是上传图片的步骤
现在说一下pc跟其他的区别
wx.chooseImage中返回
两者并不一样,为了兼容,所以在wx.uploadFile的时候不要header