小程序上传文件至云存储

 1 addImg:function(){
 2     wx.chooseImage({
 3       success: function(res) {   //chooseImage方法调用成功
 4         console.log(res);
 5 
 6         wx.cloud.uploadFile({
 7           cloudPath:"funny/mbp.jpg",
 8           filePath: res.tempFilePaths[0],           
 9             success(res){     //上传至云存储后
10             console.log(res);
11           }
12         })
13       },
14     })
15   }

第四行的console.log(res)  , 打印后可以看到一个tempFilePaths数组,tempFilePaths是图片的本地临时文件路径列表

那么tempFilePaths[0]就是具体的文件路径。

第二个res是上传文件upLoadFile后的,打印时会看到上传成功后在云存储的fileID,如果需要用到该文件就可以直接引用它了。

原文地址:https://www.cnblogs.com/Guhongying/p/10830349.html