微信小程序文件相關(guān)操作(新建文件夾、下載、解壓、讀取等)

用到的api介紹
  • wx.downloadFile() //下載
  • wx.getFileSystemManager() //獲取小程序的文件管理器
  • FileSystemManager.unzip()//解壓
  • FileSystemManager.readdir() //讀取文件夾
  • FileSystemManager.readFile() //讀取文件
  • FileSystemManager.access()//判斷文件/目錄是否存在
  • FileSystemManager.mkdir() //創(chuàng)建文件夾
index.js
  1. onload中獲取FileSystemManger的全局唯一文件管理器

    onLoad: function (options) {
      this.FileSystemManager = wx.getFileSystemManager();
      //執(zhí)行下載
      this.downloadZipHandler();
    },
    
  2. 檢查本地文件夾是否存在,不存在執(zhí)行創(chuàng)建文件夾,然后下載。如果存在直接下載。

    本地用戶文件是從 1.7.0 版本開始新增的概念。我們提供了一個(gè)用戶文件目錄給開發(fā)者,開發(fā)者對(duì)這個(gè)目錄有完全自由的讀寫權(quán)限。通過 wx.env.USER_DATA_PATH 可以獲取到這個(gè)目錄的路徑。

    downloadZipHandler() {
        let { FileSystemManager } = this;
        let that = this;
        FileSystemManager.access({
          path: `${wx.env.USER_DATA_PATH}/kk`,
          success(res) {
            console.log('success', res)
            DownloadHandler();
          },
          fail(err) {
            console.log('fail', err)
            FileSystemManager.mkdir({
              dirPath: `${wx.env.USER_DATA_PATH}/kk`,
              success(res) {
                console.log('success', res)
                DownloadHandler();
              },
              fail(err) {
                console.log('fail', err);
              }
            })
          }
        })
        function DownloadHandler() {
          wx.downloadFile({
            
            url: 'https://localhost/book.zip',
            filePath: `${wx.env.USER_DATA_PATH}/kk/book.zip`,
            success(res) {
              console.log(res);
              that.setData({
                bookZipPath: res.filePath
              })
            },
            fail(err) {
              console.log('fail', err)
            }
          })
        }
      },
    
  3. 下載完成后,解壓到本地文件夾

    unzipHandler() {
        let { FileSystemManager } = this;
        let { bookZipPath } = this.data;
        FileSystemManager.unzip({
          zipFilePath: bookZipPath,
          targetPath: `${wx.env.USER_DATA_PATH}/jj`,
          success(res) {
            console.log(res);
          },
          fail(err) {
            console.log('fail', err)
          }
        })
      },
    
  4. 讀取文件夾里的文件列表,可以得到文件夾下的文件名列表

    readDirHandler() {
        let { FileSystemManager } = this;
        FileSystemManager.readdir({
          dirPath: `${wx.env.USER_DATA_PATH}/jj`,
          success(res) {
            console.log(res);
          },
          fail(err) {
            console.log('fail', err)
          }
        })
      },
    
  5. 讀取具體文件,可以拿到文件的內(nèi)容

    readFileHandler() {
        let { FileSystemManager } = this;
        FileSystemManager.readFile({
          filePath: `${wx.env.USER_DATA_PATH}/jj/eric.json`,
          encoding: 'utf8',
          success(res) {
            console.log(res);
            let result = JSON.parse(res.data);
            console.log(result);
          },
          fail(err) {
            console.log('fail', err)
          }
        })
      },
    

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容