将本地资源上传至云存储空间,如图片、文档、视频等,如果将相同名称的文件上传至同一路径则是覆盖写
请求参数
字段 | 说明 | 数据类型 | 默认值 | 必填 |
---|---|---|---|---|
cloudPath | 云存储路径 | String | - | Y |
filePath | 要上传文件资源的路径 | String | - | Y |
header | HTTP 请求 Header, header 中不能设置 Referer | Object | - | N |
success | 成功回调 | |||
fail | 失败回调 | |||
complete | 结束回调 |
success 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
fileID | 文件 ID | String |
statusCode | 服务器返回的 HTTP 状态码 | Number |
fail 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
errCode | 错误码 | Number |
errMsg | 错误信息,格式 apiName:fail msg | String |
返回值
如果请求参数中带有 success/fail/complete 回调中的任一个,则会返回一个 UploadTask 对象,通过 UploadTask 对象可监听上传进度变化事件,以及取消上传任务。
将图片上传到云空间
回调风格
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
wx.showLoading({
title: '上传中',
})
const filePath = res.tempFilePaths[0]
console.log(res)
// 上传图片
const cloudPath = 'my-image' + filePath.match(/\.[^.]+?$/)[0]
wx.cloud.uploadFile({
cloudPath,
filePath,
success: res => {
console.log('[上传文件] 成功:', res)
app.globalData.fileID = res.fileID
app.globalData.cloudPath = cloudPath
app.globalData.imagePath = filePath
wx.navigateTo({
url: '../storageConsole/storageConsole'
})
},
fail: e => {
console.error('[上传文件] 失败:', e)
wx.showToast({
icon: 'none',
title: '上传失败',
})
},
complete: () => {
wx.hideLoading()
}
})
},
fail: e => {
console.error(e)
}
})
从云存储空间下载文件
请求参数
字段 | 说明 | 数据类型 | 默认值 | 必填 |
---|---|---|---|---|
fileID | 云文件 ID | String | - | Y |
success | 成功回调 | |||
fail | 失败回调 | |||
complete | 结束回调 |
success 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
tempFilePath | 临时文件路径 | String |
statusCode | 服务器返回的 HTTP 状态码 | Number |
fail 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
errCode | 错误码 | Number |
errMsg | 错误信息,格式 apiName:fail msg | String |
Callback 风格
wx.cloud.downloadFile({
fileID: 'a7xzcb',
success: res => {
// get temp file path
console.log(res.tempFilePath)
},
fail: err => {
// handle error
}
})
Promise 风格
wx.cloud.downloadFile({
fileID: 'a7xzcb'
}).then(res => {
// get temp file path
console.log(res.tempFilePath)
}).catch(error => {
// handle error
})
返回值 如果请求参数中带有 success/fail/complete 回调中的任一个,则会返回一个 downloadTask 对象,通过 downloadTask 对象可监听上传进度变化事件,以及取消上传任务。
从云存储空间删除文件,一次最多 50 个
请求参数
字段 | 说明 | 数据类型 | 默认值 | 必填 |
---|---|---|---|---|
fileList | 云文件 ID 字符串数组 | String[] | - | Y |
success | 成功回调 | |||
fail | 失败回调 | |||
complete | 结束回调 |
success 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
fileList | 删除结果列表,列表中的每一个对象的定义见下表 | Object[] |
fileList 列表中的对象说明
字段 | 说明 | 数据类型 |
---|---|---|
fileID | 云文件 ID | String |
status | 状态码,0 为成功 | Number |
errMsg | 成功为 ok,失败为失败原因 | String |
fail 返回参数
字段 | 说明 | 数据类型 |
---|---|---|
errCode | 错误码 | Number |
errMsg | 错误信息,格式 apiName:fail msg | String |
将fileid为cloud://car-60a3fe.6361-car-60a3fe/my-image.png的文件sc
Callback 风格
wx.cloud.deleteFile({
fileList: ['cloud://car-60a3fe.6361-car-60a3fe/my-image.png'],
success: res => {
// handle success
console.log(res.fileList)
},
fail: err => {
// handle error
}
})
Promise 风格
wx.cloud.deleteFile({
fileList: ['cloud://car-60a3fe.6361-car-60a3fe/my-image.png']
}).then(res => {
// handle success
console.log(res.fileList)
}).catch(error => {
// handle error
})