支持端:小程序 , 云函数 , Web
从云存储空间下载文件
从云存储空间下载文件
请求参数
| 字段 | 说明 | 数据类型 | 默认值 | 必填 |
|---|---|---|---|---|
| fileID | 云文件 ID | String | - | Y |
| config | 配置 | Object | - | N |
| success | 成功回调 | |||
| fail | 失败回调 | |||
| complete | 结束回调 |
config 对象定义
| 字段 | 说明 | 数据类型 |
|---|---|---|
| env | 使用的环境 ID,填写后忽略 init 指定的环境 | String |
success 返回参数
| 字段 | 说明 | 数据类型 |
|---|---|---|
| tempFilePath | 临时文件路径 | String |
| statusCode | 服务器返回的 HTTP 状态码 | Number |
| errMsg | 成功为 downloadFile:ok,失败为失败原因 | String |
fail 返回参数
| 字段 | 说明 | 数据类型 |
|---|---|---|
| errCode | 错误码 | Number |
| errMsg | 错误信息,格式 downloadFile: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 对象可监听上传进度变化事件,以及取消上传任务。
从云存储空间下载文件
请求参数
| 字段 | 说明 | 数据类型 | 默认值 | 必填 |
|---|---|---|---|---|
| fileID | 云文件 ID | String | - | Y |
Promise 返回参数
| 字段 | 说明 | 数据类型 |
|---|---|---|
| fileContent | 文件内容 | Buffer |
| statusCode | 服务器返回的 HTTP 状态码 | Number |
错误返回参数
| 字段 | 说明 | 数据类型 |
|---|---|---|
| errCode | 错误码 | Number |
| errMsg | 错误信息,格式 apiName:fail msg | String |
Promise 风格
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
const fileID = 'xxxx'
const res = await cloud.downloadFile({
fileID: fileID,
})
const buffer = res.fileContent
return buffer.toString('utf8')
} 备案信息: 粤ICP备15087711号-2
Copyright © 2008-2025 啊嘎哇在线工具箱 All Rights.
本站所有资料来源于网络,版权归原作者所有,仅作学习交流使用,如不慎侵犯了您的权利,请联系我们。