download-file.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const demoImageFileId = require('../../../../config').demoImageFileId
  2. // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-client-api/storage/downloadFile.html
  3. const app = getApp()
  4. Page({
  5. onShareAppMessage() {
  6. return {
  7. title: '下载文件',
  8. path: 'page/cloud/pages/download-file/download-file'
  9. }
  10. },
  11. data: {
  12. fileDownloaded: false,
  13. fileId: '',
  14. filePath: '',
  15. loading: false
  16. },
  17. onLoad() {
  18. this.setData({
  19. fileId: app.globalData.fileId || demoImageFileId
  20. })
  21. },
  22. downloadFile() {
  23. const fileId = this.data.fileId
  24. if (!fileId) {
  25. return
  26. }
  27. const self = this
  28. this.setData({
  29. loading: true
  30. })
  31. wx.cloud.downloadFile({
  32. fileID: fileId,
  33. success: res => {
  34. console.log('[下载文件] 成功:', res)
  35. self.setData({
  36. fileDownloaded: true,
  37. filePath: res.tempFilePath
  38. })
  39. },
  40. fail: err => {
  41. console.error('[下载文件] 失败:', err)
  42. },
  43. complete: () => {
  44. self.setData({
  45. loading: false
  46. })
  47. }
  48. })
  49. }
  50. })