小程序本身的api 並不能實現 文件下載到 系統,只能實現下載到小程序的臨時緩存裏,那麼想要實現  下載與本地存儲 需要用到 文件下載和本地文件系統API;具體方案如下:

1.下載文件到本地

wx.downloadFile({ url: 'https://example.com/file.pdf', // 文件URL success(res) { if (res.statusCode === 200) { // 文件臨時路徑 const tempFilePath = res.tempFilePath // 將文件保存到本地 saveFileToLocal(tempFilePath) } }, fail(err) { console.error('下載失敗:', err) wx.showToast({ title: '下載失敗', icon: 'none' }) } })

2. 保存到本地文件系統

functionsaveFileToLocal(tempFilePath) { wx.getFileSystemManager().saveFile({ tempFilePath: tempFilePath, success(res) { const savedFilePath = res.savedFilePath console.log('文件保存成功:', savedFilePath) // 將文件路徑保存到全局數據或緩存中 wx.setStorageSync('downloadedFilePath', savedFilePath) wx.showToast({ title: '保存成功', icon: 'success' }) }, fail(err) { console.error('保存失敗:', err) wx.showToast({ title: '保存失敗', icon: 'none' }) } }) }

小程序文件下載與本地存儲方案

要實現小程序下載文件並能在關閉小程序後仍能訪問,需要使用微信小程序的文件下載和本地文件系統API。以下是完整的實現方案:

一、基礎實現步驟

1. 下載文件到本地

JavaScript

  
wx.downloadFile({

  url: 'https://example.com/file.pdf', // 文件URL

  success(res) {

    if (res.statusCode === 200) {

      // 文件臨時路徑

      const tempFilePath = res.tempFilePath

      // 將文件保存到本地

      saveFileToLocal(tempFilePath)

    }

  },

  fail(err) {

    console.error('下載失敗:', err)

    wx.showToast({

      title: '下載失敗',

      icon: 'none'

    })

  }

})

2. 保存到本地文件系統

JavaScript

  
functionsaveFileToLocal(tempFilePath) {

  wx.getFileSystemManager().saveFile({

    tempFilePath: tempFilePath,

    success(res) {

      const savedFilePath = res.savedFilePath

      console.log('文件保存成功:', savedFilePath)

      

      // 將文件路徑保存到全局數據或緩存中

      wx.setStorageSync('downloadedFilePath', savedFilePath)

      

      wx.showToast({

        title: '保存成功',

        icon: 'success'

      })

    },

    fail(err) {

      console.error('保存失敗:', err)

      wx.showToast({

        title: '保存失敗',

        icon: 'none'

      })

    }

  })

}

二、完整實現方案

1. 封裝下載方法

JavaScript

  
// utils/fileDownload.js

const downloadAndSaveFile = (url, fileName) => {

  return new Promise((resolve, reject) => {

    wx.showLoading({ title: '下載中...' })

    

    wx.downloadFile({

      url,

      success(res) {

        if (res.statusCode === 200) {

          const fs = wx.getFileSystemManager()

          fs.saveFile({

            tempFilePath: res.tempFilePath,

            filePath: `${wx.env.USER_DATA_PATH}/${fileName || Date.now()}`,

            success(savedRes) {

              wx.hideLoading()

              resolve(savedRes.savedFilePath)

              wx.showToast({ title: '下載成功', icon: 'success' })

            },

            fail: reject

          })

        } else {

          reject(new Error('下載失敗'))

        }

      },

      fail: reject

    })

  })

}

2. 在頁面中使用


JavaScript
 
Page({
  downloadFile() {
    downloadAndSaveFile('https://example.com/report.pdf', '年度報告.pdf')
      .then(filePatherropenFile() {
    const { filePath } = this.data
    if (filePath) {
      wx.openDocument({
        filePath,
        fileType: 'pdf',
        success() {
          console.log('打開文檔成功')
        },
        fail(err) {
          wx.showToast({ title: '打開文件失敗', icon: 'none' })
        }
      })
    }
  }
})