May 20, 2021 WeChat Mini Program Development Document
Support: small programs, cloud functions, web
Download files from cloud storage
Download files from cloud storage
Request parameters
Field | Description | The data type | The default | Required |
---|---|---|---|---|
fileID | Cloud file ID | String | - | Y |
config | Configuration | Object | - | N |
success | Successful callback | |||
fail | Failed callback | |||
complete | End the callback |
Config object definition
Field | Description | The data type |
---|---|---|
Env | Use the environment ID, fill in and ignore the environment specified by init | String |
success returns parameters
Field | Description | The data type |
---|---|---|
tempFilePath | Temporary file path | String |
statusCode | The HTTP status code returned by the server | Number |
errMsg | Success is downloadFile:ok, failure is the cause of failure | String |
Fail returns parameters
Field | Description | The data type |
---|---|---|
errCode | Error code | Number |
errMsg | Error message, format downloadFile: fail msg | String |
Callback style
wx.cloud.downloadFile({
fileID: 'a7xzcb',
success: res => {
// get temp file path
console.log(res.tempFilePath)
},
fail: err => {
// handle error
}
})
Promise style
wx.cloud.downloadFile({
fileID: 'a7xzcb'
}).then(res => {
// get temp file path
console.log(res.tempFilePath)
}).catch(error => {
// handle error
})
Return Value If either of the success/fail/complete callbacks is included in the request parameter, a downloadTask object is returned that listens for upload progress change events and cancels upload tasks.
Download files from cloud storage
Request parameters
Field | Description | The data type | The default | Required |
---|---|---|---|---|
fileID | Cloud file ID | String | - | Y |
Promise returns parameters
Field | Description | The data type |
---|---|---|
fileContent | The contents of the file | Buffer |
statusCode | The HTTP status code returned by the server | Number |
The argument was returned incorrectly
Field | Description | The data type |
---|---|---|
errCode | Error code | Number |
errMsg | Error message, format apiName:fail msg | String |
Promise style
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')
}