Skip to content

文件

saveFile

TIP

該API使用方法為: wx.saveFile(Object object)
該API小程序支持,小遊戲暫不支持

TIP

本地檔存儲的大小限制為10M。

  • 功能說明: 保存文件到本地

TIP

saveFile會把暫存文件移動,囙此調用成功後傳入的tempFilePath將不可用。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    tempFilePathstring-需要保存的文件的臨時路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    savedFilePathstring存儲後的文件路徑
  • 示例代碼:

js
  wx.chooseImage({
  success(res) {
    const tempFilePaths = res.tempFilePaths
    wx.saveFile({
      tempFilePath: tempFilePaths[0],
      success(res) {
        const savedFilePath = res.savedFilePath
      }
    })
  }
})

removeSavedFile

TIP

該API使用方法為: wx.removeSavedFile(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 删除本地緩存文件。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-需要删除的文件路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 示例代碼:

js
wx.getSavedFileList({
  success(res) {
    if (res.fileList.length > 0) {
      wx.removeSavedFile({
        filePath: res.fileList[0].filePath,
        complete(res) {
          console.log(res)
        }
      })
    }
  }
})

openDocument

TIP

該API使用方法為: wx.openDocument(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 新開頁面打開文件。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-文件路徑(本地路徑),可通過downloadFile獲得
    fileTypestring-文件類型,指定文件類型打開文件
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fileType的合法值

    說明
    pdfpdf格式
  • 示例代碼:

js
wx.downloadFile({
  // Example url, not real
  url: 'https://example.com/somefile.pdf',
  success(res) {
    const filePath = res.tempFilePath
    wx.openDocument({
      filePath,
      success(res) {
        console.log('Opened document successfully')
      }
    })
  }
})

getSavedFileList

TIP

該API使用方法為: wx.getSavedFileList(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 獲取該小程序下已保存的本地緩存文件清單。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    fileListArray.<Object>文件數組,每一項是一個FileItem
  • res.fileList的結構

    内容類型說明
    filePathstring本地路徑
    sizenumber本地文件大小,以位元組為單位
    createTimenumber文件保存時的時間戳記,從1970/01/01 08:00:00到當前時間的秒數
  • 示例代碼:

js
wx.getSavedFileList({
  success(res) {
    console.log(res.fileList)
  }
})

getSavedFilelnfo

TIP

該API使用方法為: wx.getSavedFileInfo(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 獲取本地文件的文件資訊。 此接口只能用於獲取已保存到本地的文件,若需要獲取暫存文件資訊,請使用 wx.getFileInfo() 接口

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-文件路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    sizenumber文件大小,單位B
    createTimenumber文件保存時的時間戳記,從1970/01/01 08:00:00到該時刻的秒數
  • 示例代碼:

js
wx.getSavedFileList({
  success(res) {
    console.log(res.fileList)
  }
})

getFileInfo

TIP

該API使用方法為: wx.getFileInfo(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 獲取文件資訊。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-本地文件路徑
    digestAlgorithmstring'md5'計算文件摘要的算灋
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.digestAlgorithm的合法值

    說明
    md5md5算灋
    sha1sha1算灋
  • object.success回呼函數參數: Object res。

    内容類型說明
    sizenumber本地文件大小,以位元組為單位
    digeststring按照傳入的digestAlgorithm計算得出的文件摘要
  • 示例代碼:

js
wx.getFileInfo({
  success(res) {
    console.log(res.size)
    console.log(res.digest)
  }
})

getFileSystemManager

TIP

該API使用方法為: FileSystemManager wx.getFileSystemManager()

  • 功能說明: 獲取全域唯一的文件管理器
  • 返回值: FileSystemManager 文件管理器。

FileSystemManager

文件管理器,可通過wx.getFileSystemManager 獲取。

.access

TIP

該API使用方法為: FileSystemManager.access(Object object)

  • 功能說明: 判斷文件/目錄是否存在

  • 參數及說明: Object object。

    内容類型預設值必填說明
    pathstring-要判斷是否存在的文件/目錄路徑。
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory ${path}文件/目錄不存在
    bad file descriptor無效的檔描述符
    permission denied許可權錯誤,文件是只讀或只寫
    permission denied, cannot access file path目標路徑無存取權限(usr目錄)
    not a directorydirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况
    Invalid argument無效參數,可以檢查length或offset是否越界
    directory not empty目錄不為空
    the maximum size of the file storage limit is exceeded存儲空間不足,或文件大小超出上限(上限100M)
    base64 encode error字元編碼轉換失敗(例如base64格式錯誤)
    data to write is empty寫入數據為空
    illegal operation on a directory不可對目錄進行此操作(例如,指定的filePath是一個已經存在的目錄)
    file already exists ${dirPath}已有同名文件或目錄
    value of length is out of range傳入的length不合法
    value of offset is out of range傳入的offset不合法
    value of position is out of rangeposition值越界
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// Determine if the file/directory exists
fs.access({
  path: `${wx.env.USER_DATA_PATH}/hello.txt`, success(res) {
  success(res) {
    // The file exists
    console.log(res)
  }, fail(res) { // File exists.
  fail(res) {
    // File does not exist or other error
    console.error(res)
  }
})

// Synchronization interface
try {
  fs.accessSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
} catch(e) {
  console.error(e)
}

.accessSync

TIP

該API使用方法為: FileSystemManager.accessSync(string path)

  • 功能說明: FileSystemManager.access 的同步版本

  • 參數及說明: string path, 要判斷是否存在的文件/目錄路徑。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// Determine if the file/directory exists
fs.access({
  path: `${wx.env.USER_DATA_PATH}/hello.txt`, success(res) {
  success(res) {
    // The file exists
    console.log(res)
  }, fail(res) { // File exists.
  fail(res) {
    // File does not exist or other error
    console.error(res)
  }
})

// Synchronization interface
try {
  fs.accessSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
} catch(e) {
  console.error(e)
}

.appendFile

TIP

該API使用方法為: FileSystemManager.appendFile(Object object)

  • 功能說明: 在文件結尾追加內容

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要追加內容的文件路徑。
    datastring/ArrayBuffer-要追加的文字或二進位數據。
    encodingstringutf-8指定寫入文件的字元編碼。
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory ${path}指定的filePath文件不存在
    fail illegal operation on a directory, open "${filePath}"指定的filePath是一個已經存在的目錄
    fail permission denied, open ${dirPath}指定的filePath路徑沒有寫許可權
    fail sdcard not mounted指定的filePath是一個已經存在的目錄
  • 示例代碼:

js
const fs = wx.getFileSystemManager()

fs.appendFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync
try {
  fs.appendFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'some text', 'utf8')
} catch(e) {
  console.error(e)
}

.appendFileSync

TIP

該API使用方法為: FileSystemManager.appendFileSync(string filePath, string|ArrayBuffer data, string encoding)

  • 功能說明: FileSystemManager.appendFile 的同步版本

  • 參數及說明:

    • string filePath: 要追加內容的文件路徑。
    • string|ArrayBuffer data: 要追加的文字或二進位數據。
    • string encoding: 指定寫入文件的字元編碼。
  • encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • 示例代碼:

js
const fs = wx.getFileSystemManager()

fs.appendFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync
try {
  fs.appendFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'some text', 'utf8')
} catch(e) {
  console.error(e)
}

.close

TIP

該API使用方法為: FileSystemManager.close(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 關閉文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// open file
fs.open({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    // close file
    fs.close({
      fd: res.fd
    })
  }
})

.closeSync

TIP

該API使用方法為: undefined FileSystemManager.closeSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 同步關閉文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
  • 返回值: undefined。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})

// close file
fs.closeSync({fd: fd})

.copyFile

TIP

該API使用方法為: FileSystemManager.copyFile(Object object)

  • 功能說明: 複製文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    srcPathstring-原始文件路徑,只可以是普通文件
    destPathstring-目標文件路徑,支持本地路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
    • object.fail回呼函數參數: Object res。
      内容類型說明
      errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail permission denied, copyFile ${srcPath} -> ${destPath}指定目標文件路徑沒有寫許可權
    fail no such file or directory, copyFile ${srcPath} -> ${destPath}原始文件不存在,或目標文件路徑的上層目錄不存在
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.copyFile({
  srcPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  destPath: `${wx.env.USER_DATA_PATH}/hello_copy.txt`
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  fs.copyFileSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    `${wx.env.USER_DATA_PATH}/hello_copy.txt`
  )
} catch(e) {
  console.error(e)
}

.copyFileSync

TIP

該API使用方法為: FileSystemManager.copyFileSync(string srcPath, string destPath)

  • 功能說明: FileSystemManager.copyFile 的同步版本

  • 參數及說明:

    • string srcPath: 原始文件路徑,只可以是普通文件。
    • string destPath: 目標文件路徑。
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.copyFile({
  srcPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  destPath: `${wx.env.USER_DATA_PATH}/hello_copy.txt`
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  fs.copyFileSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    `${wx.env.USER_DATA_PATH}/hello_copy.txt`
  )
} catch(e) {
  console.error(e)
}

.getFileInfo

TIP

該API使用方法為: FileSystemManager.getFileInfo(Object object)

  • 功能說明: 獲取該小程序下的本地暫存文件或本地緩存文件資訊。
  • 參數及說明: Object object。
    内容類型預設值必填說明
    filePathstring-要讀取的文件路徑
    digestAlgorithmstringmd5計算文件摘要的算灋
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
    • object.digestAlgorithm的合法值
      說明
      md5md5算灋
      sha1sha1算灋
  • object.success回呼函數參數: Object res。
    内容類型說明
    sizenumber本地文件大小,以位元組為單位
    digeststring按照傳入的digestAlgorithm計算得出的文件摘要
  • object.fail回呼函數參數: Object res。
    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值
    說明
    fail file not exist指定的filePath找不到文件
    no such file or directory ${path}文件/目錄不存在,或者目標文件路徑的上層目錄不存在
    Input/output error輸入輸出流不可用
    permission denied許可權錯誤,文件是只讀或只寫
    Path permission denied傳入的路徑沒有許可權
    not a directorydirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况
    Invalid argument無效參數,可以檢查length或offset是否越界
    excced max concurrent fd limitfd數量已達上限

.fstat

TIP

該API使用方法為: FileSystemManager.fstat(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 獲取文件的狀態資訊。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    statsstatsStats對象,包含了文件的狀態資訊。
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// open files
fs.open({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    // get information
    fs.fstat({
      fd: res.fd,
      success(res) {
        console.log(res.stats)
      }
    })
  }
})

.fstatSync

TIP

該API使用方法為: Stats FileSystemManager.fstatSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 同步獲取文件的狀態資訊。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
  • 返回值: Stats, Stats對象,包含了文件的狀態資訊。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
const stats = fs.fstatSync({fd: fd})
console.log(stats)

.ftruncate

TIP

該API使用方法為: FileSystemManager.ftruncate(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 對文件內容進行截斷操作。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    lengthnumber-截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0')
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// Open the file
fs.open({
  filePath: ${wx.env.USER_DATA_PATH}/hello.txt,
  flag: 'a+',
      success(res) {
//Truncate the file content.
    fs.ftruncate({
      fd: res.fd,
  length: 10, // Truncate the file starting from the 10th byte
      success(res) {
    console.log(res)
  }
})
  }
})

.ftruncateSync

TIP

該API使用方法為: undefined FileSystemManager.ftruncateSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 對文件內容進行截斷操作。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    lengthnumber-截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0')
  • 參數及說明: undefined。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
fs.ftruncateSync({
  fd: fd,
  length: 10 // from the 10th byte
})

.getSavedFileList

TIP

該API使用方法為: FileSystemManager.getSavedFileList(Object object)

  • 功能說明: 獲取該小程序下已保存的本地緩存文件清單。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    fileListArray.<Object>文件數組,每一項是一個FileItem
  • res.fileList的結構

    内容類型說明
    filePathstring本地路徑
    sizenumber本地文件大小,以位元組為單位
    createTimenumber文件保存時的時間戳記,從1970/01/01 08:00:00到當前時間的秒數

.mkdir

TIP

該API使用方法為: FileSystemManager.mkdir(Object object)

  • 功能說明: 判斷文件/目錄是否存在

  • 參數及說明: Object object。

    内容類型預設值必填說明
    dirPathstring-創建的目錄路徑
    recursivebooleanfalse是否在遞迴創建該目錄的上級目錄後再創建該目錄。 如果對應的上級目錄已經存在,則不創建該上級目錄。 如dirPath為a/b/c/d且recursive為true,將創建a目錄,再在a目錄下創建b目錄,以此類推直至創建a/b/c目錄下的d目錄。
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory ${dirPath}上級目錄不存在
    fail permission denied, open ${dirPath}指定的filePath路徑沒有寫許可權
    fail file already exists ${dirPath}有同名文件或目錄
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.mkdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  fs.mkdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
} catch(e) {
  console.error(e)
}

.mkdirSync

TIP

該API使用方法為: FileSystemManager.mkdirSync(string dirPath, boolean recursive)

  • 功能說明: FileSystemManager.mkdir 的同步版本

  • 參數及說明:

    • string dirPath:創建的目錄路徑
    • boolean recursive:是否在遞迴創建該目錄的上級目錄後再創建該目錄。 如果對應的上級目錄已經存在,則不創建該上級目錄。 如dirPath為a/b/c/d且recursive為true,將創建a目錄,再在a目錄下創建b目錄,以此類推直至創建a/b/c目錄下的d目錄。
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.mkdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  fs.mkdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
} catch(e) {
  console.error(e)
}

.open

TIP

該API使用方法為: FileSystemManager.open(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 打開文件,返回檔描述符

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-文件路徑(本地路徑)
    flagstringr文件系統標誌,預設值: r
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • flag合法值

    合法值說明
    a打開文件用於追加。 如果文件不存在,則創建該文件
    ax類似於'a',但如果路徑存在,則失敗
    a+打開文件用於讀取和追加。 如果文件不存在,則創建該文件
    ax+類似於'a+',但如果路徑存在,則失敗
    as打開文件用於追加(在同步模式中)。 如果文件不存在,則創建該文件
    as+打開文件用於讀取和追加(在同步模式中)。 如果文件不存在,則創建該文件
    r打開文件用於讀取。 如果文件不存在,則會發生异常
    r+打開文件用於讀取和寫入。 如果文件不存在,則會發生异常
    w打開文件用於寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件
    wx類似於'w',但如果路徑存在,則失敗
    w+打開文件用於讀取和寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件
    wx+類似於'w+',但如果路徑存在,則失敗
  • object.success回呼函數參數: Object res。

    内容類型說明
    fdstring檔描述符
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.open({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    console.log(res.fd)
  }
})

.openSync

TIP

該API使用方法為: string FileSystemManager.openSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 同步打開文件,返回檔描述符。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-文件路徑(本地路徑)
    flagstringr文件系統標誌,預設值: r
  • flag合法值

    合法值說明
    a打開文件用於追加。 如果文件不存在,則創建該文件
    ax類似於'a',但如果路徑存在,則失敗
    a+打開文件用於讀取和追加。 如果文件不存在,則創建該文件
    ax+類似於'a+',但如果路徑存在,則失敗
    as打開文件用於追加(在同步模式中)。 如果文件不存在,則創建該文件
    as+打開文件用於讀取和追加(在同步模式中)。 如果文件不存在,則創建該文件
    r打開文件用於讀取。 如果文件不存在,則會發生异常
    r+打開文件用於讀取和寫入。 如果文件不存在,則會發生异常
    w打開文件用於寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件
    wx類似於'w',但如果路徑存在,則失敗
    w+打開文件用於讀取和寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件
    wx+類似於'w+',但如果路徑存在,則失敗
  • 返回值: string,檔描述符

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
console.log(fd)

.read

TIP

該API使用方法為: FileSystemManager.read(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 讀文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    arrayBufferArrayBuffer-數據寫入的緩衝區,必須是ArrayBuffer實例
    offsetnumber-緩衝區中的寫入偏移量,默認0
    lengthnumber-要從文件中讀取的位元組數,默認0
    positionnumberr文件讀取的起始位置,如不傳或傳null,則會從當前文件指針的位置讀取,如果position是正整數,則文件指針位置會保持不變並從position讀取文件。
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    bytesReadnumber實際讀取的位元組數
    arrayBufferArrayBuffer被寫入的緩存區的對象,即接口入參的arrayBuffer
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const ab = new ArrayBuffer(1024)
// open the file
fs.open({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    // read file to ArrayBuffer
    fs.read({
      fd: res.fd,
      arrayBuffer: ab,
      length: 10,
      success(res) {
        console.log(res)
      }
    })
  }
})

.readSync

TIP

該API使用方法為: ReadResult FileSystemManager.readSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 讀文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    arrayBufferArrayBuffer-數據寫入的緩衝區,必須是ArrayBuffer實例
    offsetnumber-緩衝區中的寫入偏移量,默認0
    lengthnumber-要從文件中讀取的位元組數,默認0
    positionnumberr文件讀取的起始位置,如不傳或傳null,則會從當前文件指針的位置讀取,如果position是正整數,則文件指針位置會保持不變並從position讀取文件。
  • 返回值: ReadResult 文件讀取結果。 通過

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const ab = new ArrayBuffer(1024)
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
const res = fs.readSync({
  fd: fd,
  arrayBuffer: ab,
  length: 10
})
console.log(res)

.readCompressedFile

TIP

該API使用方法為: FileSystemManager.readCompressedFile(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 讀取指定壓縮類型的本地文件內容。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要讀取的文件的路徑(本地用戶文件或代碼包文件)
    compressionAlgorithmstringr文件壓縮類型,現時僅支持br
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.compressionAlgorithm的合法值如下:

    合法值說明
    brbrotli壓縮檔
  • object.success回呼函數參數: Object res。

    内容類型說明
    dataArrayBuffer文件內容
  • 示例代碼:

js
const fs = wx.getFileSystemManager()

// 
fs.readCompressedFile({
  filePath: '${wx.env.USER_DATA_PATH}/hello.br',
  compressionAlgorithm: 'br',
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.log('readCompressedFile fail', res)
  }
})

// async api
const data = fs.readCompressedFileSync({
  filePath: '${wx.env.USER_DATA_PATH}/hello.br',
  compressionAlgorithm: 'br',
})
console.log(data)

.readCompressedFileSync

TIP

該API使用方法為: ArrayBuffer FileSystemManager.readCompressedFileSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 同步讀取指定壓縮類型的本地文件內容。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要讀取的文件的路徑(本地用戶文件或代碼包文件)
    compressionAlgorithmstringr文件壓縮類型,現時僅支持br
  • object.compressionAlgorithm的合法值如下:

    合法值說明
    brbrotli壓縮檔
  • 返回值: ArrayBuffer,文件讀取結果

  • 示例代碼:

js
const fs = wx.getFileSystemManager()

// 
fs.readCompressedFile({
  filePath: '${wx.env.USER_DATA_PATH}/hello.br',
  compressionAlgorithm: 'br',
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.log('readCompressedFile fail', res)
  }
})

// async api
const data = fs.readCompressedFileSync({
  filePath: '${wx.env.USER_DATA_PATH}/hello.br',
  compressionAlgorithm: 'br',
})
console.log(data)

.readdir

TIP

該API使用方法為: FileSystemManager.readdir(Object object)

  • 功能說明: 讀取目錄內文件清單

  • 參數及說明: Object object。

    内容類型預設值必填說明
    dirPathstring-要讀取的目錄路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    filesArray.<string>指定目錄下的檔名數組。
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory ${dirPath}目錄不存在
    fail not a directory ${dirPath}dirPath不是目錄
    fail permission denied, open ${dirPath}指定的filePath路徑沒有讀許可權
  • 示例代碼:

TIP

readdir接口無法訪問文件系統根路徑(wxfile://)。

js
const fs = wx.getFileSystemManager()
fs.readdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  success(res) {
    console.log(res.files)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.readdirSync(`${wx.env.USER_DATA_PATH}/example`)
  console.log(res)
} catch(e) {
  console.error(e)
}

.readdirSync

TIP

該API使用方法為: Array.<string> FileSystemManager.readdirSync(string dirPath)
該API小程序支持,小遊戲暫不支持

  • 功能說明: FileSystemManager.readdir的同步版本

  • 參數及說明: string dirPath, 要讀取的目錄路徑。

  • 返回值: Array.<string> files, 指定目錄下的檔名數組。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.readdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  success(res) {
    console.log(res.files)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.readdirSync(`${wx.env.USER_DATA_PATH}/example`)
  console.log(res)
} catch(e) {
  console.error(e)
}

.readFile

TIP

該API使用方法為: FileSystemManager.readFile(Object object)

  • 功能說明: 讀取本地文件內容。 單個文件大小上限為100M

  • 參數及說明: Object object

    内容類型預設值必填說明
    filePathstring-要讀取的文件的路徑
    encodingstring-指定讀取文件的字元編碼,如果不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • object.success回呼函數參數: Object res。

    内容類型說明
    datastring/ArrayBuffer文件內容
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory, open ${filePath}指定的filePath所在目錄不存在
    fail permission denied, open ${dirPath}指定的filePath路徑沒有讀許可權
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.readFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  encoding: 'utf8',
  position: 0,
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
  console.log(res)
} catch(e) {
  console.error(e)
}

.readFileSync

TIP

該API使用方法為: string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding)
該API小程序支持,小遊戲暫不支持

  • 功能說明: FileSystemManager.readFile讀取本地文件內容。 單個文件大小上限為100M

  • 參數及說明:

    • string filePath:要讀取的文件的路徑
    • string encoding:指定讀取文件的字元編碼,如果不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • 返回值: string|ArrayBuffer data,讀取本地文件內容

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.readFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  encoding: 'utf8',
  position: 0,
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})

// async api
try {
  const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
  console.log(res)
} catch(e) {
  console.error(e)
}

.readZipEntry

TIP

該API使用方法為: FileSystemManager.readZipEntry(Object object)

  • 功能說明: 讀取壓縮包內的文件

  • 參數及說明: Object object

    内容類型預設值必填說明
    filePathstring-要讀取的壓縮包的路徑(本地路徑)
    encodingstring-統一指定讀取文件的字元編碼,只在entries值為'all'時有效。 如果entries值為'all'且不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容
    entriesArray.<Object>/'all'-要讀取的壓縮包內的文件清單(當傳入'all'時表示讀取壓縮包內所有文件)
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • entries結構内容

    結構内容類型預設值必填說明
    pathstring-壓縮包內文件路徑
    encodingstring-指定讀取文件的字元編碼,如果不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容,合法取值同encoding
    positionnumber-從文件指定位置開始讀,如果不指定,則從文件頭開始讀。 讀取的範圍應該是左閉右開區間 position,position+length。有效範圍: [0, fileLength - 1]。 單位: byte
    lengthnumber-指定文件的長度,如果不指定,則讀到文件末尾。 有效範圍: [1, fileLength]。 單位: byte
  • object.success回呼函數參數: Object res。

    内容類型說明
    entriesObject文件讀取結果。 res.entries是一個對象,key是文件路徑,value是一個對象FileItem,表示該文件的讀取結果。 每個FileItem包含data(文件內容)和errMsg(錯誤資訊)内容
    • entries結構内容
    結構内容類型說明
    pathstring文件路徑
    • path結構内容
    結構内容類型說明
    datastring/ArrayBuffer文件內容
    errMsgstring錯誤資訊
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// Read one or multiple files within a zip.
fs.readZipEntry({
  filePath: 'wxfile://from/to.zip',
  entries: [{
 path: 'some_folder/my_file.txt', // File path within the zip
encoding: 'utf-8', // Specifies the character encoding for reading the file. If 'encoding' is not passed, the file's binary content is read in ArrayBuffer format
position: 0, // Start reading from a specified position in the file; if not specified, reading begins from the start of the file. The reading range should be a left-closed and right-open interval [position, position+length). The valid range is [0, fileLength - 1] in bytes.
    length: 10000, // Specify the length of the file. If not specified, reading continues until the end of the file. The valid range is [1, fileLength] in bytes.
  }, {
    path: 'other_folder/orther_file.txt', // File path within the zip
  }],
      success(res) {
    console.log(res.entries)
    // res.entries === {
    //     'some_folder/my_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //     },
    //     'other_folder/orther_file.txt': {
    //         data: (ArrayBuffer)
    //  }
    //  }
  },
  fail(res) {
    console.log(res.errMsg)
  },
})

// Reads all files within the zip. A unified 'encoding' can be specified. 'Position' and 'length' can no longer be specified and default to 0 and the file length, respectively.
fs.readZipEntry({
  filePath: 'wxfile://from/to.zip',
  entries: 'all'
encoding: 'utf-8', // Uniformly specifies the character encoding for reading the file. If 'encoding' is not passed, the file's binary content is read in ArrayBuffer format.
      success(res) {
    console.log(res.entries)
    // res.entries === {
    //     'some_folder/my_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //     },
    //     'other_folder/orther_file.txt': {
    //         errMsg: 'readZipEntry:ok',
    //         data: 'xxxxxx'
    //  }
    //  }
  },
  fail(res) {
    console.log(res.errMsg)
  },
})

.removeSavedFile

TIP

該API使用方法為: FileSystemManager.removeSavedFile(Object object)

  • 功能說明: 删除該小程序下已保存的本地緩存文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-需要删除的文件路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail file not exist指定的tempFilePath找不到文件

.rename

TIP

該API使用方法為: FileSystemManager.rename(Object object)

  • 功能說明: 重命名文件。 可以把文件從oldPath移動到newPath

  • 參數及說明: Object object。

    内容類型預設值必填說明
    oldPathstring-原始文件路徑,可以是普通文件或目錄,支持本地路徑
    newPathstring-新文件路徑,支持本地路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail permission denied, rename ${oldPath} -> ${newPath}指定原始文件或目標文件沒有寫許可權
    fail no such file or directory, rename ${oldPath} -> ${newPath}原始文件不存在,或目標文件路徑的上層目錄不存在
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.rename({
  oldPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  newPath: `${wx.env.USER_DATA_PATH}/hello_new.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.renameSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    `${wx.env.USER_DATA_PATH}/hello_new.txt`
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

.renameSync

TIP

該API使用方法為: FileSystemManager.renameSync(string oldPath, string newPath)
該API小程序支持,小遊戲暫不支持

  • 功能說明: FileSystemManager.rename的同步版本

  • 參數及說明:

    • string oldPath:原始文件路徑,可以是普通文件或目錄,支持本地路徑
    • string newPath:新文件路徑,支持本地路徑
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.rename({
  oldPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  newPath: `${wx.env.USER_DATA_PATH}/hello_new.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// async api
try {
  const res = fs.renameSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    `${wx.env.USER_DATA_PATH}/hello_new.txt`
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

.rmdir

TIP

該API使用方法為: FileSystemManager.rmdir(Object object)

  • 功能說明: 删除目錄

  • 參數及說明: Object object。

    内容類型預設值必填說明
    dirPathstring-要删除的目錄路徑
    recursivebooleanfalse是否遞迴删除目錄。 如果為true,則删除該目錄和該目錄下的所有子目錄以及文件
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory ${dirPath}目錄不存在
    fail directory not empty目錄不為空
    fail permission denied, open ${dirPath}指定的dirPath路徑沒有寫許可權
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.rmdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.rmdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
  console.log(res)
} catch(e) {
  console.error(e)
}

.rmdirSync

TIP

該API使用方法為: FileSystemManager.rmdirSync(string dirPath, boolean recursive)

  • 功能說明: FileSystemManager.rmdir 的同步版本

  • 參數及說明:

    • string dirPath: 要删除的目錄路徑
    • boolean recursive:是否遞迴删除目錄。 如果為true,則删除該目錄和該目錄下的所有子目錄以及文件
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.rmdir({
  dirPath: `${wx.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync api
try {
  const res = fs.rmdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
  console.log(res)
} catch(e) {
  console.error(e)
}

.saveFile

TIP

該API使用方法為: FileSystemManager.saveFile(Object object)

  • 功能說明: 保存暫存文件到本地。 此接口會移動暫存文件,囙此調用成功後,tempFilePath將不可用

  • 參數及說明: Object object。

    内容類型預設值必填說明
    tempFilePathstring-臨時存儲文件路徑
    filePathstring-要存儲的文件路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    savedFilePathnumber存儲後的文件路徑
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail tempFilePath file not exist指定的tempFilePath找不到文件
    fail permission denied, open "${filePath}"指定的filePath路徑沒有寫許可權
    fail no such file or directory "${dirPath}"上級目錄不存在

.saveFileSync

TIP

該API使用方法為: number FileSystemManager.saveFileSync(string tempFilePath, string filePath)

  • 功能說明: FileSystemManager.saveFile的同步版本
  • 參數及說明:
    • string tempFilePath:臨時存儲文件路徑
    • string filePath:要存儲的文件路徑
  • 返回值: number savedFilePath,存儲後的文件路徑

.stat

TIP

該API使用方法為: FileSystemManager.stat(Object object)

  • 功能說明: 獲取文件Stats對象

  • 參數及說明: Object object。

    内容類型預設值必填說明
    pathstring-文件/目錄路徑
    recursivebooleanfalse是否遞迴獲取目錄下的每個文件的Stats資訊
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.success回呼函數參數: Object res。

    内容類型說明
    statsStats/Object當recursive為false時,res.stats是一個Stats對象。 當recursive為true且path是一個目錄的路徑時,res.stats是一個Object,key以path為根路徑的相對路徑,value是該路徑對應的Stats對象
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail permission denied, open ${path}指定的path路徑沒有讀許可權
    fail no such file or directory ${path}文件不存在
  • 示例代碼:

    recursive為false時

js
//  synchronous
let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})
// sync
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, false)
recursive為true時
js
let fs = wx.getFileSystemManager()
// sync
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})
// synchronous
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, true)

.statSync

TIP

該API使用方法為: Stats|Object FileSystemManager.statSync(string path, boolean recursive)

  • 功能說明: FileSystemManager.stat 的同步版本

  • 參數及說明:

    • string path:文件/目錄路徑
    • boolean recursive:是否遞迴獲取目錄下的每個文件的Stats資訊
  • 參數及說明: Stats|Object stats,當recursive為false時,res.stats是一個Stats對象。 當recursive為true且path是一個目錄的路徑時,res.stats是一個Object,key以path為根路徑的相對路徑,value是該路徑對應的Stats對象

  • 示例代碼:

    recursive為false時

js
//  synchronous
let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})
// sync
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, false)
recursive為true時
js
let fs = wx.getFileSystemManager()
// syn
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})
// synchronous
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, true)

.truncate

TIP

該API使用方法為: FileSystemManager.truncate(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 對文件內容進行截斷操作。

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要截斷的文件路徑(本地路徑)
    lengthnumber0截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0')
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.truncate({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  length: 10, // from ten bytes to the end
  success(res) {
    console.log(res)
  }
})

.truncateSync

TIP

該API使用方法為: undefined FileSystemManager.truncateSync(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 對文件內容進行截斷操作(truncate的同步版本)

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要截斷的文件路徑(本地路徑)
    lengthnumber0截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0')
  • 返回值: undefined。

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.truncateSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  length: 10,  // from ten bytes to the end
})

TIP

該API使用方法為: FileSystemManager.unlink(Object object)

  • 功能說明: 刪除文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-需要删除的文件路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail permission denied, open ${path}指定的path路徑沒有讀許可權
    fail no such file or directory ${path}文件不存在
    fail operation not permitted, unlink ${filePath}傳入的filePath是一個目錄
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.unlink({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// sync
try {
  const res = fs.unlinkSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
  console.log(res)
} catch(e) {
  console.error(e)
}

.unlinkSync

TIP

該API使用方法為: FileSystemManager.unlinkSync(string filePath)

  • 功能說明: FileSystemManager.unlink 的同步版本

  • 參數及說明: string filePath: 需要删除的文件路徑

  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.unlink({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

try {
  const res = fs.unlinkSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
  console.log(res)
} catch(e) {
  console.error(e)
}

.unzip

TIP

該API使用方法為: FileSystemManager.unzip(Object object)

  • 功能說明: 解壓文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    zipFilePathstring-原始文件路徑,只可以是zip壓縮檔
    targetPathstring-目標目錄路徑
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail permission denied, open ${path}指定目標文件路徑沒有寫許可權
    fail no such file or directory ${path}原始文件不存在,或目標文件路徑的上層目錄不存在
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.unzip({
  zipFilePath: `${wx.env.USER_DATA_PATH}/example.zip`,
  targetPath: '${wx.env.USER_DATA_PATH}/example',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

.write

TIP

該API使用方法為: FileSystemManager.write(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 寫入文件

  • 參數及說明: Object object

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    datastring/ArrayBuffer-要寫入的文字或二進位數據
    offsetnumber-只在data類型是ArrayBuffer時有效,决定arrayBuffe中要被寫入的部位,即arrayBuffer中的索引,默認0
    lengthnumber-只在data類型是ArrayBuffer時有效,指定要寫入的位元組數,默認為arrayBuffer從0開始偏移offset個位元組後剩餘的位元組數
    encodingstringutf8指定寫入文件的字元編碼。
    positionnumberr指定文件開頭的偏移量,即數據要被寫入的位置。 當position不傳或者傳入非Number類型的值時,數據會被寫入當前指針所在位置
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • object.success回呼函數參數: Object res。

    内容類型說明
    bytesWrittennumber實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同)
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
// open the file
fs.open({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    // write the file
    fs.write({
      fd: res.fd,
      data: 'some text',
      success(res) {
        console.log(res.bytesWritten)
      }
    })
  }
})

.writeSync

TIP

該API使用方法為: FileSystemManager.writeFile(Object object)
該API小程序支持,小遊戲暫不支持

  • 功能說明: 同步寫入文件

  • 參數及說明: Object object

    内容類型預設值必填說明
    fdstring-需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得
    datastring/ArrayBuffer-要寫入的文字或二進位數據
    offsetnumber-只在data類型是ArrayBuffer時有效,决定arrayBuffe中要被寫入的部位,即arrayBuffer中的索引,默認0
    lengthnumber-只在data類型是ArrayBuffer時有效,指定要寫入的位元組數,默認為arrayBuffer從0開始偏移offset個位元組後剩餘的位元組數
    encodingstringutf8指定寫入文件的字元編碼。
    positionnumberr指定文件開頭的偏移量,即數據要被寫入的位置。 當position不傳或者傳入非Number類型的值時,數據會被寫入當前指針所在位置
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • object.success回呼函數參數: Object res。

    内容類型說明
    bytesWrittennumber實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同)
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+'
})
const res = fs.writeSync({
  fd: fd,
  data: 'some text'
})
console.log(res.bytesWritten)

.writeFile

TIP

該API使用方法為: FileSystemManager.writeFile(Object object)

  • 功能說明: 寫文件

  • 參數及說明: Object object。

    内容類型預設值必填說明
    filePathstring-要寫入的文件路徑
    datastring/ArrayBuffer-要寫入的文字或二進位數據
    encodingstringutf-8指定寫入文件的字元編碼。
    successFunction-接口調用成功的回呼函數
    failFunction-接口調用失敗的回呼函數
    completeFunction-接口調用結束的回呼函數(無論成功與否都執行)
  • object.encoding的合法值

    說明
    ascii-
    base64-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • object.fail回呼函數參數: Object res。

    内容類型說明
    errMsgstring錯誤資訊
  • res.errMsg的合法值

    說明
    fail no such file or directory, open ${filePath}指定的filePath所在目錄不存在
    fail permission denied, open ${dirPath}指定的filePath路徑沒有寫許可權
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.writeFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text or arrayBuffer',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// 
try {
  const res = fs.writeFileSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    'some text or arrayBuffer',
    'utf8'
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

.writeFileSync

TIP

該API使用方法為: FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding)

  • 功能說明: FileSystemManager.writeFile 的同步版本

  • 參數及說明:

    • string filePath:要寫入的文件路徑
    • string|ArrayBuffer data:要寫入的文字或二進位數據
    • string encoding:指定寫入文件的字元編碼。
  • object.encoding的合法值

    說明
    ascii-
    base64(注意,選擇base64編碼,data只需要傳base64內容本身,不要傳Data URI首碼,否則會報fail base64 encode error錯誤。 例如,傳aGVsbG8=而不是傳data: image/png; base64,aGVsbG8= )-
    binary-
    hex-
    ucs2/ucs-2/utf16le/utf-16le以小端序讀取
    utf-8/utf8-
    latin1-
  • 示例代碼:

js
const fs = wx.getFileSystemManager()
fs.writeFile({
  filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text or arrayBuffer',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

try {
  const res = fs.writeFileSync(
    `${wx.env.USER_DATA_PATH}/hello.txt`,
    'some text or arrayBuffer',
    'utf8'
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

錯誤碼

TIP

若無特殊說明,錯誤碼均以下錶為准

錯誤碼錯誤資訊說明
1300001operation not permitted操作不被允許(例如,filePath預期傳入一個文件而實際傳入一個目錄)
1300002no such file or directory ${path}文件/目錄不存在,或者目標文件路徑的上層目錄不存在
1300005Input/output error輸入輸出流不可用
1300009bad file descriptor無效的檔描述符
1300013permission denied許可權錯誤,文件是只讀或只寫
1300014Path permission denied傳入的路徑沒有許可權
1300020not a directorydirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况
1300021Is a directory指定路徑是一個目錄
1300022Invalid argument無效參數,可以檢查length或offset是否越界
1300036File name too long檔名過長
1300066directory not empty目錄不為空
1300201system error系統接口調用失敗
1300202the maximum size of the file storage limit is exceeded存儲空間不足,或文件大小超出上限(上限100M)
1300203base64 encode error字元編碼轉換失敗(例如base64格式錯誤)
1300300sdcard not mountedandroid sdcard掛載失敗
1300301unable to open as fileType無法以fileType打開文件
1301000permission denied, cannot access file path目標路徑無存取權限(usr目錄)
1301002data to write is empty寫入數據為空
1301003illegal operation on a directory不可對目錄進行此操作(例如,指定的filePath是一個已經存在的目錄)
1301004illegal operation on a package directory不可對代碼包目錄進行此操作
1301005file already exists ${dirPath}已有同名文件或目錄
1301006value of length is out of range傳入的length不合法
1301007value of offset is out of range傳入的offset不合法
1301009value of position is out of rangeposition值越界
1301100store directory is emptystore目錄為空
1301102unzip open file fail壓縮檔打開失敗
1301103unzip entry fail解壓單個文件失敗
1301104unzip fail解壓失敗
1301111brotli decompress failbrotli解壓失敗(例如,指定的compressionAlgorithm與文件實際壓縮格式不符)
1301112tempFilePath file not exist指定的tempFilePath找不到文件
1302001fail permission denied指定的fd路徑沒有讀許可權/沒有寫許可權
1302002exceed max concurrent fd limitfd數量已達上限
1302003invalid flag無效的flag
1302004permission denied when open using flag無法使用flag標誌打開文件
1302005array buffer does not exist未傳入arrayBuffer
1302100array buffer is readonlyarrayBuffer只讀

Stats

描述文件狀態的對象

内容

  • number mode:文件的類型和存取的許可權,對應POSIX stat.st_mode
  • number size:文件大小,單位: B, 對應POSIX stat.st_size
  • number lastAccessedTime:文件最近一次被存取或被執行的時間,UNIX時間戳記,對應POSIX stat.st_atime
  • number lastModifiedTime:文件最後一次被修改的時間,UNIX時間戳記,對應POSIX stat.st_mtime

方法集

.isDirectory

TIP

該API使用方法為: boolean Stats.isDirectory()

  • 功能說明: 判斷當前文件是否一個目錄
  • 返回值: boolean,表示當前文件是否一個目錄

.isFile

TIP

該API使用方法為: boolean Stats.isFile()

  • 功能說明: 判斷當前文件是否一個普通文件
  • 返回值: boolean,表示當前文件是否一個普通文件

ReadResult

  • 功能說明: 文件讀取結果。 通過 FileSystemManager.readSync 接口返回
  • 内容
    • number bytesRead,實際讀取的位元組數
    • ArrayBuffer arrayBuffer,被寫入的緩存區的對象,即接口入參的arrayBuffer

WriteResult

  • 功能說明: 文件讀取結果。 通過 FileSystemManager.writeSync 接口返回
  • 内容 number bytesWritten,實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同)