小程序上传图片至云存储空间
yunImg.wxml
<button bindtap="addPhoto">选择图片</button>
yunImg.js
Page({
// 选择你要上传的图片
addPhoto(){
wx.chooseImage({
count: 1, // 可以选择多少张图片
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'], // 设置图片来源
success: res => {
// tempFilePath可以作为img标签的src属性显示图片
this.uploadMage(res.tempFilePaths[0])
}
})
},
// 直接上传图片到云存储
uploadMage(temFile){
wx.cloud.uploadFile({
cloudPath: 'readNow.jpg', // 云存储路径
filePath: temFile // 要上传的文件资源路径
}).then(res => {
console.log(res);
}).catch(error => {
console.log(error);
})
}
})