1.配置云开发
打开云开发  拷贝环境ID  在全局app.js中配置 
2.界面编写
编写一个前端界面
<view>
<button type="primary" bindtap="uploadimg">点我上传</button>
</view>
绑定事件函数
uploadimg(){
wx.chooseImage({
count: 1,
success (res) {
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
wx.cloud.uploadFile({
cloudPath: 'img/example.png',
filePath: tempFilePaths[0],
success: res => {
console.log(res.fileID)
},
fail: console.error
})
}
})
},
注:这里面用到了两个API,wx.chooseImage 用来选择一张或几张图片,wx.cloud.uploadFile 用来上传选中的文件 

测试: 此时我们选中一张图片后就可以上传到云存储的对应文件夹内了  在存储里面点击刷新就出现了我配置的img文件夹了  里面正是我刚刚上传的图片  接下来要做的就是把选择后的图片显示在界面上
3.把选中的图片显示在界面上
前端页面优化
<view>
<button type="primary" bindtap="uploadimg">点我上传</button>
<text>图片预览:</text>
<image src="{{selctedimg}}"></image>
</view>
js代码优化
data: {
selctedimg:'',
},
uploadimg(){
let _that=this
wx.chooseImage({
count: 1,
success (res) {
const tempFilePaths = res.tempFilePaths
const fileName=res.tempFilePaths[0].slice(11)
wx.cloud.uploadFile({
cloudPath: 'img/'+fileName+'.png',
filePath: tempFilePaths[0],
success: res => {
console.log(res.fileID)
_that.setData({
selctedimg:res.fileID
})
},
fail: console.error
})
}
})
},
测试: 现在就实现了上传那张图片就可以实时预览那张图片了!   
4.手机上测试
当然也可以在手机上测试该上传功能     
|