Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API file file file manager


May 19, 2021 WeChat Mini Program Development Document


Table of contents


FileSystemManager

Base library 1.9.9 starts to be supported, and low versions need to be compatible.

The file manager


Method:

FileSystemManager.access(Object object)

Determine if a file/directory exists

Parameters

Object object

Property Type The default Required Description
path string Is File/directory path to determine whether it exists (local path)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory ${path} The file/directory does not exist



FileSystemManager.accessSync(string path)

FileSystemManager.access Synchronization Version

parameter

string path

To determine if there is a file / directory path (local path)

mistake

error code Error message illustrate
fail no such file or directory ${path} File / directory does not exist


FileSystemManager.appendFile(Object object)

Bastronomy 2.1.0 began support, low version needs to do Compatibility

Add content at the end of the file

Parameters

Object object

Property Type The default Required Description
filePath string Is File path to append content (local path)
data string/ArrayBuffer Is Text or binary data to append
encoding string utf8 Whether Specifies the character encoding to be written to the file
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

The legal value of object.encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory, open ${filePath} The specified filePath file does not exist
fail illegal operation on a directory, open "${filePath}" The filePath specified is an existing directory
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail sdcard not mounted The filePath specified is an existing directory


FileSystemManager.appendFileSync(string filePath, string| ArrayBuffer data, string encoding)

Base library 2.1.0 starts to support, and low versions need to be compatible.

A synchronized version of FileSystemManager.appendFile

Parameters

string filePath

File path to append content (local path)

string| ArrayBuffer data

Text or binary data to append

string encoding

Specifies the character encoding to be written to the file

The legal value of encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

Error

Error code The error message Description
fail no such file or directory, open ${filePath} The specified filePath file does not exist
fail illegal operation on a directory, open "${filePath}" The filePath specified is an existing directory
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail sdcard not mounted The filePath specified is an existing directory


FileSystemManager.copyFile(Object object)

Copy the file

Parameters

Object object

Property Type The default Required Description
srcPath string Is The source file path, which supports the local path
destPath string Is The target file path, which supports the local path
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail permission denied, copyFile ${srcPath} -> ${destPath} The specified target file path does not have write permissions
fail no such file or directory, copyFile ${srcPath} -> ${destPath} The source file does not exist, or the upper directory of the target file path does not exist
fail the maximum size of the file storage limit is exceeded There is not enough storage space


FileSystemManager.copyFileSync(string srcPath, string destPath)

A synchronized version of FileSystemManager.copyFile

Parameters

string srcPath

The source file path, which supports the local path

string destPath

The target file path, which supports the local path

Error

Error code The error message Description
fail permission denied, copyFile ${srcPath} -> ${destPath} The specified target file path does not have write permissions
fail no such file or directory, copyFile ${srcPath} -> ${destPath} The source file does not exist, or the upper directory of the target file path does not exist
fail the maximum size of the file storage limit is exceeded There is not enough storage space


FileSystemManager.getFileInfo(Object object)

Gets the local temporary file or local cache file information under the small program

Parameters

Object object

Property Type The default Required Description
filePath string Is File path to read (local path)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.success callback function

Parameters
Object res
Property Type Description
size number File size, in bytes

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail file not exist The specified filePath could not find the file


FileSystemManager.getSavedFileList(Object object)

Gets a list of local cached files saved under the small program

Parameters

Object object

Property Type The default Required Description
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.success callback function

Parameters
Object res
Property Type Description
fileList Array.<Object> An array of files

The structure of res.fileList

Property Type Description
filePath string File path (local path)
size number The local file size, in bytes
createTime number The timestamp at the time the file was saved, from 1970/01/01 08:00:00 to the number of seconds of the current time


FileSystemManager.mkdir(Object object)

Create a directory

Parameters

Object object

Property Type The default Required Description The lowest version
dirPath string Is Directory path created (local path)
recursive boolean false Whether Whether to create the directory after recursively creating the parent directory of the directory. I f the corresponding parent directory already exists, the parent directory is not created. If dirPath is a/b/c/d and recursive is true, an a directory is created, then a directory is created under directory a, and so on, until the d directory under the a/b/c directory is created. 2.3.0
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory ${dirPath} The parent directory does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail file already exists ${dirPath} There is a file or directory with the same name


FileSystemManager.mkdirSync(string dirPath, boolean recursive)

A synchronized version of FileSystemManager.mkdir

Parameters

string dirPath

Directory path created (local path)

boolean recursive

The base library 2.3.0 starts to support, and the lower version needs to be compatible.

Whether to create the directory after recursively creating the parent directory of the directory. I f the corresponding parent directory already exists, the parent directory is not created. If dirPath is a/b/c/d and recursive is true, an a directory is created, then a directory is created under directory a, and so on, until the d directory under the a/b/c directory is created.

Error

Error code The error message Description
fail no such file or directory ${dirPath} The parent directory does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail file already exists ${dirPath} There is a file or directory with the same name


FileSystemManager.readdir(Object object)

Read the list of files in the directory

Parameters

Object object

Property Type The default Required Description
dirPath string Is Directory path to read (local path)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.success callback function

Parameters
Object res
Property Type Description
files Array.<string> Specify an array of file names in the directory.

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory ${dirPath} The directory does not exist
fail not a directory ${dirPath} DirPath is not a directory
fail permission denied, open ${dirPath} The specified filePath path does not have read permissions


Array.<string> FileSystemManager.readdirSync(string dirPath)

A synchronized version of FileSystemManager.readdir

Parameters

string dirPath

Directory path to read (local path)

Returns a value

Array.<string> files

Specify an array of file names in the directory.

Error

Error code The error message Description
fail no such file or directory ${dirPath} The directory does not exist
fail not a directory ${dirPath} DirPath is not a directory
fail permission denied, open ${dirPath} The specified filePath path does not have read permissions


FileSystemManager.readFile(Object object)

Read the contents of the local file

Parameters

Object object

Property Type The default Required Description The lowest version
filePath string Is Path to the file to read (local path)
encoding string Whether Specifies the character encoding of the read file, and if encoding is not passed, the binary content of the file is read in ArrayBuffer format
position string Whether Read from the file's specified location, or, if not, from the file header. T he range of the read should be the left-closed-right open interval . E ffective range: s0, fileLength - 1. Unit: byte 2.10.0
length string Whether Specify the length of the file and, if not, read to the end of the file. E ffective range: s1, fileLength. Unit: byte 2.10.0
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

The legal value of object.encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

object.success callback function

Parameters
Object res
Property Type Description
data string/ArrayBuffer The contents of the file

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory, open ${filePath} The directory where the specified filePath is located does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have read permissions


string| ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding, string position, string length)

A synchronized version of FileSystemManager.readFile

Parameters

string filePath

Path to the file to read (local path)

string encoding

Specifies the character encoding of the read file, and if encoding is not passed, the binary content of the file is read in ArrayBuffer format

The legal value of encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

string position

Base library 2.10.0 starts to support, and low versions need to be compatible.

Read from the file's specified location, or, if not, from the file header. T he range of the read should be the left-closed-right open interval . E ffective range: s0, fileLength - 1. Unit: byte

string length

Base library 2.10.0 starts to support, and low versions need to be compatible.

Specify the length of the file and, if not, read to the end of the file. E ffective range: s1, fileLength. Unit: byte

Returns a value

string| ArrayBuffer data

The contents of the file

Error

Error code The error message Description
fail no such file or directory, open ${filePath} The directory where the specified filePath is located does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have read permissions


FileSystemManager.removeSavedFile(Object object)

Delete the local cache files that have been saved under the small program

Parameters

Object object

Property Type The default Required Description
filePath string Is File paths that need to be deleted (local paths)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail file not exist The specified tempFilePath could not find the file


FileSystemManager.rename(Object object)

Rename the file. You can move files from oldPath to newPath

Parameters

Object object

Property Type The default Required Description
oldPath string Is The source file path, which supports the local path
newPath string Is The new file path supports the local path
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail permission denied, rename ${oldPath} -> ${newPath} The specified source or destination file does not have write permissions
fail no such file or directory, rename ${oldPath} -> ${newPath} The source file does not exist, or the upper directory of the target file path does not exist


FileSystemManager.renameSync(string oldPath, string newPath)

A synchronized version of FileSystemManager.rename

Parameters

string oldPath

The source file path, which supports the local path

string newPath

The new file path supports the local path

Error

Error code The error message Description
fail permission denied, rename ${oldPath} -> ${newPath} The specified source or destination file does not have write permissions
fail no such file or directory, rename ${oldPath} -> ${newPath} The source file does not exist, or the upper directory of the target file path does not exist


FileSystemManager.rmdir(Object object)

Delete the directory

Parameters

Object object

Property Type The default Required Description The lowest version
dirPath string Is Directory path to delete (local path)
recursive boolean false Whether Whether to recursively delete the directory. If true, the directory and all subdirections and files under that directory are deleted. 2.3.0
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory ${dirPath} The directory does not exist
fail directory not empty The directory is not empty
fail permission denied, open ${dirPath} The specified dirPath path does not have write permissions


FileSystemManager.rmdirSync(string dirPath, boolean recursive)

A synchronized version of FileSystemManager.rmdir

Parameters

string dirPath

Directory path to delete (local path)

boolean recursive

The base library 2.3.0 starts to support, and the lower version needs to be compatible.

Whether to recursively delete the directory. If true, the directory and all subdirections and files under that directory are deleted.

Error

Error code The error message Description
fail no such file or directory ${dirPath} The directory does not exist
fail directory not empty The directory is not empty
fail permission denied, open ${dirPath} The specified dirPath path does not have write permissions


FileSystemManager.saveFile(Object object)

Save temporary files locally. This interface moves the temporary file, so when the call is successful, tempFilePath will not be available.

Parameters

Object object

Property Type The default Required Description
tempFilePath string Is Temporary storage file path (local path)
filePath string Whether File path to store (local path)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.success callback function

Parameters
Object res
Property Type Description
savedFilePath string Stored file path (local path)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail tempFilePath file not exist The specified tempFilePath could not find the file
fail permission denied, open "${filePath}" The specified filePath path does not have write permissions
fail no such file or directory "${dirPath}" The parent directory does not exist
fail the maximum size of the file storage limit is exceeded There is not enough storage space


string FileSystemManager.saveFileSync(string tempFilePath, string filePath)

A synchronized version of FileSystemManager.saveFile

Parameters

string tempFilePath

Temporary storage file path (local path)

string filePath

File path to store (local path)

Returns a value

string savedFilePath

Stored file path (local path)

Error

Error code The error message Description
fail tempFilePath file not exist The specified tempFilePath could not find the file
fail permission denied, open "${filePath}" The specified filePath path does not have write permissions
fail no such file or directory "${dirPath}" The parent directory does not exist
fail the maximum size of the file storage limit is exceeded There is not enough storage space


FileSystemManager.stat(Object object)

Gets the file Stats object

Parameters

Object object

Property Type The default Required Description The lowest version
path string Is File/Directory Path (Local Path)
recursive boolean false Whether Whether to recursively get Stats information for each file in the directory 2.3.0
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.success callback function

Parameters
Object res
Property Type Description
stats Stats/Object When recursive is false, res.stats is a Stats object. When recursive is true and path is a path to a directory, res.stats is an Object, the relative path of the key with path as the root path, and value is the Stats object corresponding to that path.

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail permission denied, open ${path} The specified path path does not have read permissions
fail no such file or directory ${path} The file does not exist

The sample code

When the recursive is false


let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})

When the recursive is true


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())
    })
  }
})


Stats| Object FileSystemManager.statSync(string path, boolean recursive)

A synchronized version of FileSystemManager.stat

Parameters

string path

File/Directory Path (Local Path)

boolean recursive

The base library 2.3.0 starts to support, and the lower version needs to be compatible.

Whether to recursively get Stats information for each file in the directory

Returns a value

Stats| Object stats

When recursive is false, res.stats is a Stats object. When recursive is true and path is a path to a directory, res.stats is an Object, the relative path of the key with path as the root path, and value is the Stats object corresponding to that path.

Error

Error code The error message Description
fail permission denied, open ${path} The specified path path does not have read permissions
fail no such file or directory ${path} The file does not exist

The sample code

When the recursive is false


let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})

When the recursive is true


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())
    })
  }
})


FileSystemManager.unlink(Object object)

Delete the file

Parameters

Object object

Property Type The default Required Description
filePath string Is File path to delete (local path)
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg


Value Description The lowest version
fail permission denied, open ${path} The specified path path does not have read permissions
fail no such file or directory ${path} The file does not exist
fail operation not permitted, unlink ${filePath} The incoming filePath is a directory


FileSystemManager.unlinkSync(string filePath)

A synchronized version of FileSystemManager.unlink

Parameters

string filePath

File path to delete (local path)

Error

Error code The error message Description
fail permission denied, open ${path} The specified path path does not have read permissions
fail no such file or directory ${path} The file does not exist
fail operation not permitted, unlink ${filePath} The incoming filePath is a directory


FileSystemManager.unzip(Object object)

Unzim the file

Parameters

Object object

Property Type The default Required Description
zipFilePath string Is The source file path, which supports the local path, can only be zip compressed files
targetPath string Is The destination directory path, which supports local paths
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail permission denied, unzip ${zipFilePath} -> ${destPath} The specified target file path does not have write permissions
fail no such file or directory, unzip ${zipFilePath} -> "${destPath} The source file does not exist, or the upper directory of the target file path does not exist


FileSystemManager.writeFile(Object object)

Write a file

Parameters

Object object

Property Type The default Required Description
filePath string Is File path to write to (local path)
data string/ArrayBuffer Is The text or binary data to write
encoding string utf8 Whether Specifies the character encoding to be written to the file
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

The legal value of object.encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
fail no such file or directory, open ${filePath} The directory where the specified filePath is located does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail the maximum size of the file storage limit is exceeded There is not enough storage space


FileSystemManager.writeFileSync(string filePath, string| ArrayBuffer data, string encoding)

A synchronized version of FileSystemManager.writeFile

Parameters

string filePath

File path to write to (local path)

string| ArrayBuffer data

The text or binary data to write

string encoding

Specifies the character encoding to be written to the file

The legal value of encoding

Value Description The lowest version
Ascii
base64
binary
hex
ucs2 Read in small-end order
ucs-2 Read in small-end order
utf16le Read in small-end order
utf-16le Read in small-end order
utf-8
utf8
latin1

Error

Error code The error message Description
fail no such file or directory, open ${filePath} The directory where the specified filePath is located does not exist
fail permission denied, open ${dirPath} The specified filePath path does not have write permissions
fail the maximum size of the file storage limit is exceeded There is not enough storage space