上传到云存储的方法
function uploadFile(cloudParentPath, filePath){
var temps = filePath.split('.')
var cloudPath = cloudParentPath + app.globalData.openid + '_' + new Date().getTime() + '.' + temps[temps.length -1]
console.log(cloudPath)
return new Promise((resolve, reject) => {
wx.cloud.uploadFile({
cloudPath: cloudPath, // 上传至云端的路径
filePath: filePath, // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log(res)
resolve(res.fileID)
},
fail: error => {
console.error(error)
reject(error)
}
})
})
}
wxml
<mp-uploader bindfail="uploadError" bindsuccess="uploadSuccess" select="{{selectFile}}" upload="{{uplaodFile}}" files="{{files}}" max-count="3" title="图片上传" tips="图片上传提示"></mp-uploader>
我遇到的报upload file fail, urls not found错误原因 是直接resolve(res)
查看开发文档https://developers.weixin.qq.com/miniprogram/dev/extended/weui/uploader.html
所以需要把返回的路径地址放到数组中
js里 uploadFile
uplaodFile(files) {
console.log('upload files', files)
// 文件上传的函数,返回一个promise
let that = this
return new Promise((resolve, reject) => {
util.cloudFunction.uploadFile('space/pr/', files.tempFilePaths[0]).then(res => {
console.log(res)
var urls = [res]
resolve({urls})
}).catch(error => {
reject('some error')
})
})
},
注意cloudPath不能以/开头,而且上传的路径文件夹必须已经存在。