- addImg : function(){
- wx.chooseImage({// 选择图片
- count : 1, // 规定选择图片的数量, 默认 9
- sizeType : ["original","compressed"], // 规定图片的尺寸, 原图 / 压缩图
- sourceType : ['album','camera'], // 从哪里选择图片, 相册 / 相机
- success : (chooseres)=>{ // 接口调用成功的时候执行的函数
- //console.log(chooseres);
- // 选择图片后可以在这里上传
- wx.cloud.uploadFile({
- cloudPath: "img/" + new Date().getTime() +"-"+ Math.floor(Math.random() * 1000),// 云储存的路径及文件名
- filePath : chooseres.tempFilePaths[0], // 要上传的图片 / 文件路径 这里使用的是选择图片返回的临时地址
- success : (uploadres) => { // 上传图片到云储存成功
- //console.log(uploadres)
- wx.showLoading({ // 显示加载提示框 不会自动关闭 只能 wx.hideLoading 关闭
- title : "图片上传中", // 提示框显示的提示信息
- mask : true, // 显示透明蒙层, 防止触摸. 为 true 提示的时候不可以对屏幕进行操作, 不写或为 false 时可以操作屏幕
- success : function () {
- setTimeout(function(){ // 使用定时让提示框消失
- wx.hideLoding() // 让提示框隐藏, 消失
- }, 1000); // 定时时间 (ms) 这里表示消息提示框显示后 1s 执行 wx.hideLoding(), 即消息提示框显示 1 秒
- }
- });
- },
- fail : (err) => {
- console.log(err)
- }
- })
- },
- fail : (err) => {
- console.log(err)
- }
- })
- }
来源: http://www.bubuko.com/infodetail-3355882.html