文件
saveFile
TIP
該API使用方法為: wx.saveFile(Object object)
該API小程序支持,小遊戲暫不支持
TIP
本地檔存儲的大小限制為10M。
- 功能說明: 保存文件到本地
TIP
saveFile會把暫存文件移動,囙此調用成功後傳入的tempFilePath將不可用。
參數及說明: Object object。
内容 類型 預設值 必填 說明 tempFilePath string - 是 需要保存的文件的臨時路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 savedFilePath string 存儲後的文件路徑 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 需要删除的文件路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 文件路徑(本地路徑),可通過downloadFile獲得 fileType string - 否 文件類型,指定文件類型打開文件 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fileType的合法值
值 說明 pdf pdf格式 示例代碼:
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。
内容 類型 預設值 必填 說明 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 fileList Array.< Object>文件數組,每一項是一個FileItem res.fileList的結構
内容 類型 說明 filePath string 本地路徑 size number 本地文件大小,以位元組為單位 createTime number 文件保存時的時間戳記,從1970/01/01 08:00:00到當前時間的秒數 示例代碼:
wx.getSavedFileList({
success(res) {
console.log(res.fileList)
}
})getSavedFilelnfo
TIP
該API使用方法為: wx.getSavedFileInfo(Object object)
該API小程序支持,小遊戲暫不支持
功能說明: 獲取本地文件的文件資訊。 此接口只能用於獲取已保存到本地的文件,若需要獲取暫存文件資訊,請使用 wx.getFileInfo() 接口
參數及說明: Object object。
内容 類型 預設值 必填 說明 filePath string - 是 文件路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 size number 文件大小,單位B createTime number 文件保存時的時間戳記,從1970/01/01 08:00:00到該時刻的秒數 示例代碼:
wx.getSavedFileList({
success(res) {
console.log(res.fileList)
}
})getFileInfo
TIP
該API使用方法為: wx.getFileInfo(Object object)
該API小程序支持,小遊戲暫不支持
功能說明: 獲取文件資訊。
參數及說明: Object object。
内容 類型 預設值 必填 說明 filePath string - 是 本地文件路徑 digestAlgorithm string 'md5' 否 計算文件摘要的算灋 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.digestAlgorithm的合法值
值 說明 md5 md5算灋 sha1 sha1算灋 object.success回呼函數參數: Object res。
内容 類型 說明 size number 本地文件大小,以位元組為單位 digest string 按照傳入的digestAlgorithm計算得出的文件摘要 示例代碼:
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。
内容 類型 預設值 必填 說明 path string - 是 要判斷是否存在的文件/目錄路徑。 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory ${ path}文件/目錄不存在 bad file descriptor 無效的檔描述符 permission denied 許可權錯誤,文件是只讀或只寫 permission denied, cannot access file path 目標路徑無存取權限(usr目錄) not a directory dirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况 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 range position值越界 示例代碼:
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, 要判斷是否存在的文件/目錄路徑。
示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要追加內容的文件路徑。 data string/ArrayBuffer - 是 要追加的文字或二進位數據。 encoding string utf-8 否 指定寫入文件的字元編碼。 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 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是一個已經存在的目錄 示例代碼:
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 - 示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) 示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 返回值: undefined。
示例代碼:
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。
内容 類型 預設值 必填 說明 srcPath string - 是 原始文件路徑,只可以是普通文件 destPath string - 是 目標文件路徑,支持本地路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) - object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊
- object.fail回呼函數參數: Object res。
res.errMsg的合法值
值 說明 fail permission denied, copyFile ${ srcPath} -> ${destPath}指定目標文件路徑沒有寫許可權 fail no such file or directory, copyFile ${ srcPath} -> ${destPath}原始文件不存在,或目標文件路徑的上層目錄不存在 示例代碼:
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: 目標文件路徑。
示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要讀取的文件路徑 digestAlgorithm string md5 否 計算文件摘要的算灋 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) - object.digestAlgorithm的合法值
值 說明 md5 md5算灋 sha1 sha1算灋
- object.digestAlgorithm的合法值
- object.success回呼函數參數: Object res。
内容 類型 說明 size number 本地文件大小,以位元組為單位 digest string 按照傳入的digestAlgorithm計算得出的文件摘要 - object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 - res.errMsg的合法值
值 說明 fail file not exist 指定的filePath找不到文件 no such file or directory ${ path}文件/目錄不存在,或者目標文件路徑的上層目錄不存在 Input/output error 輸入輸出流不可用 permission denied 許可權錯誤,文件是只讀或只寫 Path permission denied 傳入的路徑沒有許可權 not a directory dirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况 Invalid argument 無效參數,可以檢查length或offset是否越界 excced max concurrent fd limit fd數量已達上限
.fstat
TIP
該API使用方法為: FileSystemManager.fstat(Object object)
該API小程序支持,小遊戲暫不支持
功能說明: 獲取文件的狀態資訊。
參數及說明: Object object。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 stats stats Stats對象,包含了文件的狀態資訊。 示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 返回值: Stats, Stats對象,包含了文件的狀態資訊。
示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 length number - 是 截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0') success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) 示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 length number - 是 截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0') 參數及說明: undefined。
示例代碼:
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。
内容 類型 預設值 必填 說明 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 fileList Array.< Object>文件數組,每一項是一個FileItem res.fileList的結構
内容 類型 說明 filePath string 本地路徑 size number 本地文件大小,以位元組為單位 createTime number 文件保存時的時間戳記,從1970/01/01 08:00:00到當前時間的秒數
.mkdir
TIP
該API使用方法為: FileSystemManager.mkdir(Object object)
功能說明: 判斷文件/目錄是否存在
參數及說明: Object object。
内容 類型 預設值 必填 說明 dirPath string - 是 創建的目錄路徑 recursive boolean false 否 是否在遞迴創建該目錄的上級目錄後再創建該目錄。 如果對應的上級目錄已經存在,則不創建該上級目錄。 如dirPath為a/b/c/d且recursive為true,將創建a目錄,再在a目錄下創建b目錄,以此類推直至創建a/b/c目錄下的d目錄。 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory ${ dirPath}上級目錄不存在 fail permission denied, open ${ dirPath}指定的filePath路徑沒有寫許可權 fail file already exists ${ dirPath}有同名文件或目錄 示例代碼:
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目錄。
示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 文件路徑(本地路徑) flag string r 否 文件系統標誌,預設值: r success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) flag合法值
合法值 說明 a 打開文件用於追加。 如果文件不存在,則創建該文件 ax 類似於'a',但如果路徑存在,則失敗 a+ 打開文件用於讀取和追加。 如果文件不存在,則創建該文件 ax+ 類似於'a+',但如果路徑存在,則失敗 as 打開文件用於追加(在同步模式中)。 如果文件不存在,則創建該文件 as+ 打開文件用於讀取和追加(在同步模式中)。 如果文件不存在,則創建該文件 r 打開文件用於讀取。 如果文件不存在,則會發生异常 r+ 打開文件用於讀取和寫入。 如果文件不存在,則會發生异常 w 打開文件用於寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件 wx 類似於'w',但如果路徑存在,則失敗 w+ 打開文件用於讀取和寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件 wx+ 類似於'w+',但如果路徑存在,則失敗 object.success回呼函數參數: Object res。
内容 類型 說明 fd string 檔描述符 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 文件路徑(本地路徑) flag string r 否 文件系統標誌,預設值: r flag合法值
合法值 說明 a 打開文件用於追加。 如果文件不存在,則創建該文件 ax 類似於'a',但如果路徑存在,則失敗 a+ 打開文件用於讀取和追加。 如果文件不存在,則創建該文件 ax+ 類似於'a+',但如果路徑存在,則失敗 as 打開文件用於追加(在同步模式中)。 如果文件不存在,則創建該文件 as+ 打開文件用於讀取和追加(在同步模式中)。 如果文件不存在,則創建該文件 r 打開文件用於讀取。 如果文件不存在,則會發生异常 r+ 打開文件用於讀取和寫入。 如果文件不存在,則會發生异常 w 打開文件用於寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件 wx 類似於'w',但如果路徑存在,則失敗 w+ 打開文件用於讀取和寫入。 如果文件不存在則創建文件,如果文件存在則截斷文件 wx+ 類似於'w+',但如果路徑存在,則失敗 返回值: string,檔描述符
示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 arrayBuffer ArrayBuffer - 是 數據寫入的緩衝區,必須是ArrayBuffer實例 offset number - 否 緩衝區中的寫入偏移量,默認0 length number - 否 要從文件中讀取的位元組數,默認0 position number r 否 文件讀取的起始位置,如不傳或傳null,則會從當前文件指針的位置讀取,如果position是正整數,則文件指針位置會保持不變並從position讀取文件。 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 bytesRead number 實際讀取的位元組數 arrayBuffer ArrayBuffer 被寫入的緩存區的對象,即接口入參的arrayBuffer 示例代碼:
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。
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 arrayBuffer ArrayBuffer - 是 數據寫入的緩衝區,必須是ArrayBuffer實例 offset number - 否 緩衝區中的寫入偏移量,默認0 length number - 否 要從文件中讀取的位元組數,默認0 position number r 否 文件讀取的起始位置,如不傳或傳null,則會從當前文件指針的位置讀取,如果position是正整數,則文件指針位置會保持不變並從position讀取文件。 返回值: ReadResult 文件讀取結果。 通過
示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要讀取的文件的路徑(本地用戶文件或代碼包文件) compressionAlgorithm string r 是 文件壓縮類型,現時僅支持br success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.compressionAlgorithm的合法值如下:
合法值 說明 br brotli壓縮檔 object.success回呼函數參數: Object res。
内容 類型 說明 data ArrayBuffer 文件內容 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要讀取的文件的路徑(本地用戶文件或代碼包文件) compressionAlgorithm string r 是 文件壓縮類型,現時僅支持br object.compressionAlgorithm的合法值如下:
合法值 說明 br brotli壓縮檔 返回值: ArrayBuffer,文件讀取結果
示例代碼:
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。
内容 類型 預設值 必填 說明 dirPath string - 是 要讀取的目錄路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 files Array.< string>指定目錄下的檔名數組。 object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory ${ dirPath}目錄不存在 fail not a directory ${ dirPath}dirPath不是目錄 fail permission denied, open ${ dirPath}指定的filePath路徑沒有讀許可權 示例代碼:
TIP
readdir接口無法訪問文件系統根路徑(wxfile://)。
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, 指定目錄下的檔名數組。示例代碼:
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
内容 類型 預設值 必填 說明 filePath string - 是 要讀取的文件的路徑 encoding string - 否 指定讀取文件的字元編碼,如果不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - object.success回呼函數參數: Object res。
内容 類型 說明 data string/ArrayBuffer 文件內容 object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory, open ${ filePath}指定的filePath所在目錄不存在 fail permission denied, open ${ dirPath}指定的filePath路徑沒有讀許可權 示例代碼:
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,讀取本地文件內容
示例代碼:
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
内容 類型 預設值 必填 說明 filePath string - 是 要讀取的壓縮包的路徑(本地路徑) encoding string - 否 統一指定讀取文件的字元編碼,只在entries值為'all'時有效。 如果entries值為'all'且不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容 entries Array.< Object>/'all'- 是 要讀取的壓縮包內的文件清單(當傳入'all'時表示讀取壓縮包內所有文件) success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - entries結構内容
結構内容 類型 預設值 必填 說明 path string - 是 壓縮包內文件路徑 encoding string - 否 指定讀取文件的字元編碼,如果不傳encoding,則以ArrayBuffer格式讀取文件的二進位內容,合法取值同encoding position number - 否 從文件指定位置開始讀,如果不指定,則從文件頭開始讀。 讀取的範圍應該是左閉右開區間 position,position+length。有效範圍: [0, fileLength - 1]。 單位: byte length number - 否 指定文件的長度,如果不指定,則讀到文件末尾。 有效範圍: [1, fileLength]。 單位: byte object.success回呼函數參數: Object res。
内容 類型 說明 entries Object 文件讀取結果。 res.entries是一個對象,key是文件路徑,value是一個對象FileItem,表示該文件的讀取結果。 每個FileItem包含data(文件內容)和errMsg(錯誤資訊)内容 - entries結構内容
結構内容 類型 說明 path string 文件路徑 - path結構内容
結構内容 類型 說明 data string/ArrayBuffer 文件內容 errMsg string 錯誤資訊 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 需要删除的文件路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail file not exist 指定的tempFilePath找不到文件
.rename
TIP
該API使用方法為: FileSystemManager.rename(Object object)
功能說明: 重命名文件。 可以把文件從oldPath移動到newPath
參數及說明: Object object。
内容 類型 預設值 必填 說明 oldPath string - 是 原始文件路徑,可以是普通文件或目錄,支持本地路徑 newPath string - 是 新文件路徑,支持本地路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail permission denied, rename ${ oldPath} -> ${newPath}指定原始文件或目標文件沒有寫許可權 fail no such file or directory, rename ${ oldPath} -> ${newPath}原始文件不存在,或目標文件路徑的上層目錄不存在 示例代碼:
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:新文件路徑,支持本地路徑
示例代碼:
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。
内容 類型 預設值 必填 說明 dirPath string - 是 要删除的目錄路徑 recursive boolean false 否 是否遞迴删除目錄。 如果為true,則删除該目錄和該目錄下的所有子目錄以及文件 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory ${ dirPath}目錄不存在 fail directory not empty 目錄不為空 fail permission denied, open ${ dirPath}指定的dirPath路徑沒有寫許可權 示例代碼:
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,則删除該目錄和該目錄下的所有子目錄以及文件
示例代碼:
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。
内容 類型 預設值 必填 說明 tempFilePath string - 是 臨時存儲文件路徑 filePath string - 否 要存儲的文件路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 savedFilePath number 存儲後的文件路徑 object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 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。
内容 類型 預設值 必填 說明 path string - 是 文件/目錄路徑 recursive boolean false 否 是否遞迴獲取目錄下的每個文件的Stats資訊 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 stats Stats/Object 當recursive為false時,res.stats是一個Stats對象。 當recursive為true且path是一個目錄的路徑時,res.stats是一個Object,key以path為根路徑的相對路徑,value是該路徑對應的Stats對象 object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail permission denied, open ${ path}指定的path路徑沒有讀許可權 fail no such file or directory ${ path}文件不存在 示例代碼:
recursive為false時
// 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時
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時
// 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時
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。
内容 類型 預設值 必填 說明 filePath string - 是 要截斷的文件路徑(本地路徑) length number 0 否 截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0') success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要截斷的文件路徑(本地路徑) length number 0 否 截斷位置,默認0。 如果length小於文件長度(位元組),則只有前面length個位元組會保留在文件中,其餘內容會被删除; 如果length大於文件長度,則會對其進行擴展,並且擴展部分將填充空位元組('\0') 返回值: undefined。
示例代碼:
const fs = wx.getFileSystemManager()
fs.truncateSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
length: 10, // from ten bytes to the end
}).unlink
TIP
該API使用方法為: FileSystemManager.unlink(Object object)
功能說明: 刪除文件
參數及說明: Object object。
内容 類型 預設值 必填 說明 filePath string - 是 需要删除的文件路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail permission denied, open ${ path}指定的path路徑沒有讀許可權 fail no such file or directory ${ path}文件不存在 fail operation not permitted, unlink ${ filePath}傳入的filePath是一個目錄 示例代碼:
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: 需要删除的文件路徑
示例代碼:
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。
内容 類型 預設值 必填 說明 zipFilePath string - 是 原始文件路徑,只可以是zip壓縮檔 targetPath string - 是 目標目錄路徑 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail permission denied, open ${ path}指定目標文件路徑沒有寫許可權 fail no such file or directory ${ path}原始文件不存在,或目標文件路徑的上層目錄不存在 示例代碼:
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
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 data string/ArrayBuffer - 是 要寫入的文字或二進位數據 offset number - 否 只在data類型是ArrayBuffer時有效,决定arrayBuffe中要被寫入的部位,即arrayBuffer中的索引,默認0 length number - 否 只在data類型是ArrayBuffer時有效,指定要寫入的位元組數,默認為arrayBuffer從0開始偏移offset個位元組後剩餘的位元組數 encoding string utf8 否 指定寫入文件的字元編碼。 position number r 否 指定文件開頭的偏移量,即數據要被寫入的位置。 當position不傳或者傳入非Number類型的值時,數據會被寫入當前指針所在位置 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - object.success回呼函數參數: Object res。
内容 類型 說明 bytesWritten number 實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同) 示例代碼:
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
内容 類型 預設值 必填 說明 fd string - 是 需要被關閉的檔描述符,fd通過 FileSystemManager.open FileSystemManager.openSync 接口獲得 data string/ArrayBuffer - 是 要寫入的文字或二進位數據 offset number - 否 只在data類型是ArrayBuffer時有效,决定arrayBuffe中要被寫入的部位,即arrayBuffer中的索引,默認0 length number - 否 只在data類型是ArrayBuffer時有效,指定要寫入的位元組數,默認為arrayBuffer從0開始偏移offset個位元組後剩餘的位元組數 encoding string utf8 否 指定寫入文件的字元編碼。 position number r 否 指定文件開頭的偏移量,即數據要被寫入的位置。 當position不傳或者傳入非Number類型的值時,數據會被寫入當前指針所在位置 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - object.success回呼函數參數: Object res。
内容 類型 說明 bytesWritten number 實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同) 示例代碼:
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。
内容 類型 預設值 必填 說明 filePath string - 是 要寫入的文件路徑 data string/ArrayBuffer - 是 要寫入的文字或二進位數據 encoding string utf-8 否 指定寫入文件的字元編碼。 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.encoding的合法值
值 說明 ascii - base64 - binary - hex - ucs2/ucs-2/utf16le/utf-16le 以小端序讀取 utf-8/utf8 - latin1 - object.fail回呼函數參數: Object res。
内容 類型 說明 errMsg string 錯誤資訊 res.errMsg的合法值
值 說明 fail no such file or directory, open ${ filePath}指定的filePath所在目錄不存在 fail permission denied, open ${ dirPath}指定的filePath路徑沒有寫許可權 示例代碼:
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 - 示例代碼:
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
若無特殊說明,錯誤碼均以下錶為准
| 錯誤碼 | 錯誤資訊 | 說明 |
|---|---|---|
| 1300001 | operation not permitted | 操作不被允許(例如,filePath預期傳入一個文件而實際傳入一個目錄) |
| 1300002 | no such file or directory ${path} | 文件/目錄不存在,或者目標文件路徑的上層目錄不存在 |
| 1300005 | Input/output error | 輸入輸出流不可用 |
| 1300009 | bad file descriptor | 無效的檔描述符 |
| 1300013 | permission denied | 許可權錯誤,文件是只讀或只寫 |
| 1300014 | Path permission denied | 傳入的路徑沒有許可權 |
| 1300020 | not a directory | dirPath指定路徑不是目錄,常見於指定的寫入路徑的上級路徑為一個文件的情况 |
| 1300021 | Is a directory | 指定路徑是一個目錄 |
| 1300022 | Invalid argument | 無效參數,可以檢查length或offset是否越界 |
| 1300036 | File name too long | 檔名過長 |
| 1300066 | directory not empty | 目錄不為空 |
| 1300201 | system error | 系統接口調用失敗 |
| 1300202 | the maximum size of the file storage limit is exceeded | 存儲空間不足,或文件大小超出上限(上限100M) |
| 1300203 | base64 encode error | 字元編碼轉換失敗(例如base64格式錯誤) |
| 1300300 | sdcard not mounted | android sdcard掛載失敗 |
| 1300301 | unable to open as fileType | 無法以fileType打開文件 |
| 1301000 | permission denied, cannot access file path | 目標路徑無存取權限(usr目錄) |
| 1301002 | data to write is empty | 寫入數據為空 |
| 1301003 | illegal operation on a directory | 不可對目錄進行此操作(例如,指定的filePath是一個已經存在的目錄) |
| 1301004 | illegal operation on a package directory | 不可對代碼包目錄進行此操作 |
| 1301005 | file already exists ${dirPath} | 已有同名文件或目錄 |
| 1301006 | value of length is out of range | 傳入的length不合法 |
| 1301007 | value of offset is out of range | 傳入的offset不合法 |
| 1301009 | value of position is out of range | position值越界 |
| 1301100 | store directory is empty | store目錄為空 |
| 1301102 | unzip open file fail | 壓縮檔打開失敗 |
| 1301103 | unzip entry fail | 解壓單個文件失敗 |
| 1301104 | unzip fail | 解壓失敗 |
| 1301111 | brotli decompress fail | brotli解壓失敗(例如,指定的compressionAlgorithm與文件實際壓縮格式不符) |
| 1301112 | tempFilePath file not exist | 指定的tempFilePath找不到文件 |
| 1302001 | fail permission denied | 指定的fd路徑沒有讀許可權/沒有寫許可權 |
| 1302002 | exceed max concurrent fd limit | fd數量已達上限 |
| 1302003 | invalid flag | 無效的flag |
| 1302004 | permission denied when open using flag | 無法使用flag標誌打開文件 |
| 1302005 | array buffer does not exist | 未傳入arrayBuffer |
| 1302100 | array buffer is readonly | arrayBuffer只讀 |
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,實際被寫入到文件中的位元組數(注意,被寫入的位元組數不一定與被寫入的字串字元數相同)