微信小程序雲開發:獲取存取文件的臨時網絡地址


取臨時鏈接

可以根據文件 ID 換取臨時文件網絡鏈接,文件鏈接有有效期為兩個小時

wx.cloud.getTempFileURL({
  fileList: ['cloud://xxx.png'],
  success: res => {
    // fileList 是一個有如下結構的對象數組
    // [{
    //    fileID: 'cloud://xxx.png', // 文件 ID
    //    tempFileURL: '', // 臨時文件網絡鏈接
    //    maxAge: 120 * 60 * 1000, // 有效期
    // }]
    console.log(res.fileList)
  },
  fail: console.error
})

增查刪:

1,上傳文件
在小程序端可調用 wx.cloud.uploadFile 方法進行上傳:

wx.cloud.uploadFile({
  cloudPath: 'example.png', // 上傳至雲端的路徑
  filePath: '', // 小程序臨時文件路徑
  success: res => {
    // 返回文件 ID
    console.log(res.fileID)
  },
  fail: console.error
})
上傳成功后會獲得文件唯一標識符,即文件 ID,后續操作都基於文件 ID 而不是 URL。

2,下載文件
可以根據文件 ID 下載文件,用戶僅可下載其有訪問權限的文件:

wx.cloud.downloadFile({
  fileID: '', // 文件 ID
  success: res => {
    // 返回臨時文件路徑
    console.log(res.tempFilePath)
  },
  fail: console.error
})

3,刪除文件
可以通過 wx.cloud.deleteFile 刪除文件:

wx.cloud.deleteFile({
  fileList: ['a7xzcb'],
  success: res => {
    // handle success
    console.log(res.fileList)
  },
  fail: console.error
})

4,組件支持
支持在 image、audio 等組件中傳入雲文件 ID

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM