微信小程序云開發(fā)SDK文檔 文件存儲·下載文件

2022-05-12 16:34 更新

Cloud.downloadFile()

支持端:小程序 , 云函數(shù) , Web

從云存儲空間下載文件


wx.cloud.downloadFile

從云存儲空間下載文件

請求參數(shù)

字段 說明 數(shù)據(jù)類型 默認值 必填
fileID 云文件 ID String - Y
config 配置 Object - N
success 成功回調(diào)
fail 失敗回調(diào)
complete 結束回調(diào)

config 對象定義

字段 說明 數(shù)據(jù)類型
env 使用的環(huán)境 ID,填寫后忽略 init 指定的環(huán)境 String

success 返回參數(shù)

字段 說明 數(shù)據(jù)類型
tempFilePath 臨時文件路徑 String
statusCode 服務器返回的 HTTP 狀態(tài)碼 Number
errMsg 成功為 downloadFile:ok,失敗為失敗原因 String

fail 返回參數(shù)

字段 說明 數(shù)據(jù)類型
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
})

返回值 如果請求參數(shù)中帶有 success/fail/complete 回調(diào)中的任一個,則會返回一個 downloadTask 對象,通過 downloadTask 對象可監(jiān)聽上傳進度變化事件,以及取消上傳任務。


downloadFile

從云存儲空間下載文件

請求參數(shù)

字段說明數(shù)據(jù)類型默認值必填
fileID云文件 IDString-Y

Promise 返回參數(shù)

字段說明數(shù)據(jù)類型
fileContent文件內(nèi)容Buffer
statusCode服務器返回的 HTTP 狀態(tài)碼Number

錯誤返回參數(shù)

字段說明數(shù)據(jù)類型
errCode錯誤碼Number
errMsg錯誤信息,格式 apiName:fail msgString

使用示例

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')
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號