Skip to content

File

saveFile

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

saveFile(Object object)

Save the file locally. Note:saveFile Will move the temporary file, so the tempFilePath Will not be available

Note

saveFile will move the temporary file, so the tempFilePath passed in will not be available after a successful call.

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
tempFilePathstringyesTemporary path to the file you want to save (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
savedFilePathstringFile path after storage (Local path)

sample code

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

:: tip Note The size limit for local file storage is 10M :::

removeSavedFile

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

removeSavedFile(Object object)

Delete local cache file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
tempFilePathstringyesFile path to delete (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

sample code

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

openDocument

Supported since base library 1.3.9, iOS version 2.1.23, Android not yet supported

openDocument(Object object)

A new page opens the document. WeChat Client 7.0.12 Version of the default display of the upper right corner menu on, after the default version does not show, you need to take the initiative to pass showMenu。

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path (Local path) , through downloadFile Get
fileTypestringnoWhether to show the top right menu
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fileType Legal value

valueIntroductions
docdoc format
docxdocx format
xlsxls format
xlsxxlsx format
pptppt format
pptxpptx format
pdfpdf format

sample code

javascript
ft.downloadFile({
  // Example Url, not real
  url: 'http://example.com/somefile.pdf',
  success(res) {
    const filePath = res.tempFilePath
    ft.openDocument({
      filePath,
      success(res) {
        console.log('Document opened successfully.')
      }
    })
  }
})

getSavedFileList

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

getSavedFileList(Object object)

Gets a list of local cache files saved under the Mini Program

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
fileListArray.<Object>Array of files, each one a FileItem

res.fileList Structure

attributetypeIntroductions
filePathstringFile path (Local path)
sizenumberLocal file size in bytes
createTimenumberTime stamp when file is saved, from 1970/01/01 08:00:00 Number of seconds to the current time

sample code

javascript
ft.getSavedFileList({
  success(res) {
    console.log(res.fileList)
  }
})

getSavedFileInfo

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

getSavedFileInfo(Object object)

Gets the file information for the local file. This interface can only be used to obtain files that have been saved locally. To obtain temporary file information, use ft.getFileInfo() Interface.

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
sizenumberFile size, unit B
createTimenumberTime stamp when file is saved, from 1970/01/01 08:00:00 Number of seconds to the time

sample code

javascript
ft.getSavedFileList({
  success(res) {
    console.log(res.fileList)
  }
})

getFileInfo

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

getFileInfo(Object object)

Get File Information

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesLocal file path
digestAlgorithmstring'md5'noAlgorithm for computing file summaries
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.digestAlgorithm Legal value

valueIntroductions
md5md5 algorithm
sha1sha1 algorithm

object.success callback

parameter

Object res

attributetypeIntroductions
sizenumberFile size, in bytes
digeststringAccording to the incoming digestAlgorithm Summary of calculated documents

sample code

javascript
ft.getFileInfo({
  success(res) {
    console.log(res.size)
    console.log(res.digest)
  }
})

saveFileToDisk

Save file system files to the user disk, only in the PC End support

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesPath of the file to be saved
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

sample code

javascript
ft.saveFileToDisk({
  filePath: `${ft.env.USER_DATA_PATH}/hello.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

getFileSystemManager

Foundation Library 2.10.6 support from

getFileSystemManager()

Get the globally unique file manager

Return value

FileSystemManager

ile manager, available through the ft.getFileSystemManager Get.

FileSystemManager.access

FileSystemManager.access(Object object)

Judgment file/Directory exists

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
pathstringyesFiles to determine if they exist/Directory path (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()
// Judgment file/Directory exists
fs.access({
  path: `${ft.env.USER_DATA_PATH}/hello.txt`, 
  success(res) {
    // File existence
    console.log(res)
  },
  fail(res) {
    // File does not exist or other error
    console.error(res)
  }
})

// Synchronous interface
try {
  fs.accessSync(`${ft.env.USER_DATA_PATH}/hello.txt`)
} catch(and) {
  console.error(and)
}

FileSystemManager.accessSync

FileSystemManager.accessSync(string path)

FileSystemManager.accessThe synchronous version of

parameter

string path

Files to determine if they exist/Directory path (Local path)

sample code

javascript
const fs = ft.getFileSystemManager()
// Synchronous interface
try {
  fs.accessSync(`${ft.env.USER_DATA_PATH}/test.txt`)
} catch(e) {
  console.error(e)
}

FileSystemManager.appendFile

FileSystemManager.appendFile(Object object)

在文件结尾追加内容

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to read (Local path)
datastring/ArrayBufferyesText or binary data to be appended
encodingstringutf-8noSpecifies the character encoding for writing to the file
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.appendFile({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  data: 'this is text',
  encoding: 'utf-8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.appendFileSync

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

FileSystemManager.appendFileThe synchronised version of

parameter

string filePath

File path to append content (Local path)

string|ArrayBuffer data

Text or binary data to append

string encoding

Specifies the character encoding for writing to the file

sample code

javascript
// Synchronous interface
const fs = ft.getFileSystemManager()

try {
  fs.appendFileSync(`${ft.env.USER_DATA_PATH}/test.txt`, 'this is text', 'utf8')
} catch(e) {
  console.error(e)
}

FileSystemManager.close

FileSystemManager.close(Object object)

Close file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

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

FileSystemManager.closeSync

FileSystemManager.closeSync(Object object)

Synchronize Close Files

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition

sample code

javascript
const fs = ft.getFileSystemManager()

const fd = fs.openSync({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+'
})

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

FileSystemManager.copyFile

FileSystemManager.copyFile(Object object)

Copy file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
srcPathstringyesSource file path, support local path
destPathstringyesDestination file path, local path support
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.copyFile({
  srcPath: `${ft.env.USER_DATA_PATH}/test-txt`,
  destPath: `${ft.env.USER_DATA_PATH}/new-test.txt`
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.copyFileSync

FileSystemManager.copyFileSync(string srcPath, string destPath)

FileSystemManager.copyFileThe synchronised version of

parameter

string srcPath

Source file path, support local path

string destPath

Destination file path, local path support

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  fs.copyFileSync(
    `${ft.env.USER_DATA_PATH}/test.txt`,
    `${ft.env.USER_DATA_PATH}/new-test.txt`
  )
} catch(e) {
  console.error(e)
}

FileSystemManager.fstat

FileSystemManager.fstat(Object object)

Get status information for files

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
statsobjectStats Object that contains the state of the file

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()
// Open the file
fs.open({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+',
  success(res) {
    // Get status information for files
    fs.fstat({
      fd: res.fd,
      success(res) {
        console.log(res.stats)
      }
    })
  }
})

FileSystemManager.fstatSync

FileSystemManager.fstatSync(Object object)

Synchronize access to file status information

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition

Return value

Object object

Stats Object that contains the state of the file

sample code

javascript
const fs = ft.getFileSystemManager()

const fd = fs.openSync({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+'
})

const stats = fs.fstatSync({fd: fd})
console.log(stats)

FileSystemManager.ftruncate

FileSystemManager.ftruncate(Object object)

Truncate file contents

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
lengthnumberyesTruncate position, default 0. if length Is less than the file length in bytes, only the first length Four bytes will remain in the file and the rest will be deletedif length Is greater than the file length, it will be expanded, and the extension will be filled with empty bytes0')
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

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

FileSystemManager.ftruncateSync

FileSystemManager.ftruncateSync(Object object)

Truncate file contents

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
lengthnumberyesTruncate position, default 0. if length Is less than the file length in bytes, only the first length Four bytes will remain in the file and the rest will be deletedif length Is greater than the file length, it will be expanded, and the extension will be filled with empty bytes0')

Return value

undefined

sample code

javascript
const fs = ft.getFileSystemManager()

const fd = fs.openSync({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+'
})
fs.ftruncateSync({
  fd: fd,
  length: 10 // Truncate the file from the 10th byte
})

FileSystemManager.getFileInfo

FileSystemManager.getFileInfo(Object object)

Under the Mini Program Local temporary file or Local cache file information

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to read (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
sizenumberFile size, in bytes

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.getFileInfo({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.getSavedFileList

FileSystemManager.getSavedFileList(Object object)

Gets a list of local cache files saved under the Mini Program

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
fileListArray.<Object>File array

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.getSavedFileList({
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.mkdir

FileSystemManager.mkdir(Object object)

Create a directory

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
dirPathstringyesDirectory path created (Local path)
recursivebooleanfalsenoWhether to create this directory after recursively creating its parent directory. If the corresponding parent directory already exists, the parent directory is not created. Such as dirPath for a/b/c/d and recursive for True, will create the a Directory, and then in the a Create under directory b Directory, and so on until the a/b/c Directory d Directory.
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.mkdir({
  dirPath: `${ft.env.USER_DATA_PATH}/a/b/c`,
  recursive: false
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.mkdirSync

FileSystemManager.mkdirSync(string dirPath, boolean recursive)

FileSystemManager.mkdirSyncThe synchronised version of

parameter

string dirPath

Directory path created (Local path)

boolean recursive

Whether to create this directory after recursively creating its parent directory. If the corresponding parent directory already exists, the parent directory is not created. Such as dirPath for a/b/c/d and recursive for True, will create the a Directory, and then in the a Create under directory b Directory, and so on until the a/b/c Directory d Directory

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  fs.mkdirSync(`${ft.env.USER_DATA_PATH}/a/b`, false)
} catch(e) {
  console.error(e)
}

FileSystemManager.open

FileSystemManager.open(Object object)

Open the file and return the file descriptor

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path (Local path)
flagstringrnoFile system flag, default: 'r'
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.flag Legal value

valueIntroductions
aOpen the file for appending. If the file does not exist, create the file
axSimilar to 'a ', but fails if path exists
a+Open the file for reading and appending. If the file does not exist, create the file
ax+Similar to 'a + ', but fails if path exists
asOpen the file for appending (in sync mode). If the file does not exist, create the file
as+Open files for reading and appending (in synchronous mode). If the file does not exist, create the file
rOpen the file for reading. If the file does not exist, an exception occurs
r+Open files for reading and writing. If the file does not exist, an exception occurs
wOpen the file for writing. Create a file if it does not exist, and truncate it if it exists
wxSimilar to 'w ', but fails if path exists
w+Open files for reading and writing. Create a file if it does not exist, and truncate it if it exists
wx+Similar to 'w + ', but fails if path exists

object.success callback

parameter

Object res

attributetypeIntroductions
fdstringFile descriptor

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.open({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+',
  success(res) {
    console.log(res.fd)
  }
})

FileSystemManager.openSync

FileSystemManager.openSync(Object object)

Open file synchronously, return file descriptor

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path (Local path)
flagstringrnoFile system flag, default: 'r'

object.flag Legal value

valueIntroductions
aOpen the file for appending. If the file does not exist, create the file
axSimilar to 'a ', but fails if path exists
a+Open the file for reading and appending. If the file does not exist, create the file
ax+Similar to 'a + ', but fails if path exists
asOpen the file for appending (in sync mode). If the file does not exist, create the file
as+Open files for reading and appending (in synchronous mode). If the file does not exist, create the file
rOpen the file for reading. If the file does not exist, an exception occurs
r+Open files for reading and writing. If the file does not exist, an exception occurs
wOpen the file for writing. Create a file if it does not exist, and truncate it if it exists
wxSimilar to 'w ', but fails if path exists
w+Open files for reading and writing. Create a file if it does not exist, and truncate it if it exists
wx+Similar to 'w + ', but fails if path exists

Return value

string

File descriptor

sample code

javascript
const fs = ft.getFileSystemManager()

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

FileSystemManager.read

FileSystemManager.read(Object object)

Read Files

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
arrayBufferArrayBufferyesThe buffer to which the data is written must be ArrayBuffer Example
offsetnumber0noWrite offset in buffer, default 0
lengthnumber0noNumber of bytes to read from the file, default 0
positionnumbernoThe starting position of a file read, such as no upload or upload Null, it is read from the position of the current file pointer. if position Is a positive integer, the file pointer position remains the same and is converted from the position Read the file.
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
bytesReadnumberNumber of bytes actually read
arrayBufferArrayBufferObject that is written to the buffer, that is, the arrayBuffer

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

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

FileSystemManager.readdir

FileSystemManager.readdir(Object object)

Read list of files in a directory

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
dirPathstringyesDirectory path to read (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
filesArray.<string>Specifies an array of file names in a directory.

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.readdir({
  dirPath: `${ft.env.USER_DATA_PATH}/a`,
  success(res) {
    console.log(res.files)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.readdirSync

FileSystemManager.readdirSync(string dirPath)

FileSystemManager.readdirSyncThe synchronised version of

parameter

string dirPath

Directory path to read (Local path)

Return value

Array.<string> files

Specifies an array of file names in a directory

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  const res = fs.readdirSync(`${ft.env.USER_DATA_PATH}/a`)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.readFile

FileSystemManager.readFile(Object object)

Read local file content

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesPath to the file to read (Local path)
encodingstringutf-8noSpecifies the character encoding for the read file, if the Encoding, the ArrayBuffer Format to read the binary contents of a file
positionnumbernoStart reading from the specified location in the file or from the file header if not specified. Read range should be left closed and right open range [position, position+length)。有效范围:[0, fileLength - 1]. Unit: byte
lengthnumbernoSpecifies the length of the file, if not specified, read to the end of the document. Scope of validity:[1, fileLength]. Unit: byte
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

object.success callback

parameter

Object res

attributetypeIntroductions
datastring/ArrayBufferDocument content

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.readFile({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  encoding: 'utf8',
  position: 0,
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.readFileSync

FileSystemManager.readFileSync(string filePath, string encoding, number position, number length)

FileSystemManager.readFileSyncThe synchronised version of

parameter

string filePath

Path to the file to read (Local path)

string encoding

Specifies the character encoding for the read file, if the Encoding, the ArrayBuffer Format to read the binary contents of a file

encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

number position

Start reading from the specified location in the file or from the file header if not specified. Read range should be left closed and right open range [position, position+length)。Effective range:[0, fileLength - 1]. Unit: byte

number length

Specifies the length of the file, if not specified, read to the end of the document. Scope of validity:[1, fileLength]. Unit: byte

Return value

string|ArrayBuffer data

Document content

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  fs.copyFileSync(
    `${ft.env.USER_DATA_PATH}/test.txt`,
    `${ft.env.USER_DATA_PATH}/new-test.txt`
  )
} catch(e) {
  console.error(e)
}

FileSystemManager.readSync

FileSystemManager.readSync(Object object)

Read Files

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
arrayBufferArrayBufferyesThe buffer to which the data is written must be ArrayBuffer Example
offsetnumber0noWrite offset in buffer, default 0
lengthnumber0noNumber of bytes to read from the file, default 0
positionnumbernoThe starting position of a file read, such as no upload or upload Null, it is read from the position of the current file pointer. if position Is a positive integer, the file pointer position remains the same and is converted from the position Read the file.

Return value

Object res

attributetypeIntroductions
bytesReadnumberNumber of bytes actually read
arrayBufferArrayBufferThe buffer to which the data is written must be ArrayBuffer Example

sample code

javascript
const fs = ft.getFileSystemManager()

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

FileSystemManager.removeSavedFile

FileSystemManager.removeSavedFile(Object object)

Delete the saved local cache file under the Mini Program

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to delete (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.removeSavedFile({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.rename

FileSystemManager.rename(Object object)

Rename the file. You can transfer files from oldPath Move to newPath

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
oldPathstringyesSource file path, support local path
newPathstringyesNew file path, support local path
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.rename({
  oldPath: `${ft.env.USER_DATA_PATH}/hello.txt`,
  newPath: `${ft.env.USER_DATA_PATH}/new-hello.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.renameSync

FileSystemManager.renameSync(string oldPath, string newPath)

FileSystemManager.renameThe synchronised version of

parameter

string oldPath

Source file path, support local path

string newPath

New file path, support local path

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  const res = fs.renameSync(
    `${ft.env.USER_DATA_PATH}/hello.txt`,
    `${ft.env.USER_DATA_PATH}/new-hello.txt`
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.rmdir

FileSystemManager.rmdir(Object object)

Delete directory

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
dirPathstringyesDirectory path to delete (Local path)
recursivebooleanfalsenoWhether to recursively delete the directory. If for True, the directory and all subdirectories and files under it are deleted.
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.rmdir({
  dirPath: `${ft.env.USER_DATA_PATH}/a`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.rmdirSync

FileSystemManager.rmdirSync(string dirPath, boolean recursive)

FileSystemManager.rmdirThe synchronised version of

parameter

string dirPath

Directory path to delete (Local path)

boolean recursive

Whether to recursively delete the directory. If for True, the directory and all subdirectories and files under it are deleted

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  const res = fs.rmdirSync(`${ft.env.USER_DATA_PATH}/a`, false)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.saveFile

FileSystemManager.saveFile(Object object)

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

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
tempFilePathstringyesTemporary storage file path (Local path)
filePathstringnoFile path to store (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
savedFilePathstringFile path after storage (Local path)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.saveFile({
  tempFilePath: `finfile:://temp/sample.text`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.saveFileSync

FileSystemManager.saveFileSync(string tempFilePath, string filePath)

FileSystemManager.saveFileThe synchronised version of

parameter

string tempFilePath

Temporary storage file path (Local path)

string filePath

File path to store (Local path)

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  fs.saveFileSync( `finfile://temp/sample.txt`)
} catch(e) {
  console.error(e)
}

FileSystemManager.stat

FileSystemManager.stat(Object object)

Get file information object

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
pathstringyesFile/directory path (local path)
recursivebooleanfalsenoWhether to recursively fetch information about each file in a directory
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
statsobject/Array.<object>When recursive is false, res.stats yes an object. When recursive is true and path yes a path to a directory, res.stats yes an Array, one object per item yes of the array, each object containing path and stats.

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.stat({
  path: `${ft.env.USER_DATA_PATH}/test`,
  success: res => {
    console.log(res.stats)
  }
})

fs.stat({
  path: `${ft.env.USER_DATA_PATH}/test`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats)
    })
  }
})

FileSystemManager.statSync

FileSystemManager.statSync(string path, boolean recursive)

FileSystemManager.statThe synchronised version of

parameter

string path

file/Directory path (Local path)

string recursive

For each file in the directory Stats information

Return value

object|Array.<object>

when recursive for false When res.stats It's a... Stats Object. when recursive for true and path Is a directory path, res.stats It's a... Array, each item in an array is an object, and each object contains path and stats

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  const res = fs.statSync(`${ft.env.USER_DATA_PATH}/sample`)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.truncate

FileSystemManager.truncate(Object object)

Truncate file contents

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to truncate (Local path)
lengthnumber0yesTruncate position, default 0. if length Less than the length of the file (bytes), only the front length Four bytes will remain in the file and the rest will be deletedif length Is greater than the file length, it will be expanded, and the extension will be filled with empty bytes0')
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.truncate({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  length: 10,
  success(res) {
    console.log(res)
  }
})

FileSystemManager.truncateSync

FileSystemManager.truncateSync(Object object)

FileSystemManager.truncateThe synchronised version of

parameter

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to truncate (Local path)
lengthnumber0yesTruncate position, default 0. if length Less than the length of the file (bytes), only the front length Four bytes will remain in the file and the rest will be deletedif length Is greater than the file length, it will be expanded, and the extension will be filled with empty bytes0')

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  fs.truncateSync({
    filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
    length: 10,
  })
} catch(e) {
  console.error(e)
}

FileSystemManager.unlink(Object object)

Delete file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to delete (Local path)
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.unlink({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.unlinkSync

FileSystemManager.unlinkSync(string filePath)

FileSystemManager.unlinkThe synchronised version of

parameter

string filePath

File path to delete (Local path)

sample code

javascript

const fs = ft.getFileSystemManager()

// Synchronous interface
try {
  const res = fs.unlinkSync(`${ft.env.USER_DATA_PATH}/test.txt`)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.unzip

FileSystemManager.unzip(Object object)

Decompress file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
zipFilePathstringyesSource file path, support local path, It can only be zip Compressed file
targetPathstring/ArrayBufferyesDestination directory path, Support local path
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.unzip({
  zipFilePath: `${ft.env.USER_DATA_PATH}/sample.zip`,
  targetPath: `${ft.env.USER_DATA_PATH}/sample`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.write

FileSystemManager.write(Object object)

Write file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
datastring/ArrayBufferyesWrite content of type String or ArrayBuffer
offsetnumber0noOnly in data Type is ArrayBuffer When valid, decided arrayBuffe To be written to in the arrayBuffer Index in, default 0
lengthnumbernoOnly in data Type is ArrayBuffer Specifies the number of bytes to write, and defaults to arrayBuffer Offset from 0 offset Number of bytes remaining after 1 byte
encodingstringutf-8noOnly in data Type is String Specifies the character encoding to write to the file, and defaults to utf8
positionnumbernoSpecifies the offset at the beginning of the file, where the data will be written. when position Not passed or passed in Number Type, the data is written to the current pointer location.
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

object.success callback

parameter

Object res

attributetypeIntroductions
bytesWrittennumberNumber of bytes actually written to the file (note that the number of byte writes is not necessarily the same as the string characters written)

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

fs.open({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+',
  success(res) {
    // Write file
    fs.write({
      fd: res.fd,
      data: 'this is text',
      success(res) {
        console.log(res.bytesWritten)
      }
    })
  }
})

FileSystemManager.writeFile

FileSystemManager.writeFile(Object object)

Write file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
filePathstringyesFile path to write to (Local path)
datastring/ArrayBufferyesText or binary data to write
encodingstringutf-8noSpecifies the character encoding for writing to the file
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

object.fail callback

parameter

Object res

attributetypeIntroductions
errMsgstringError message

sample code

javascript
const fs = ft.getFileSystemManager()

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

FileSystemManager.writeFileSync

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

FileSystemManager.writeFileThe synchronised version of

parameter

string filePath

File path to write to (Local path)

string|ArrayBuffer data

Text or binary data to write

string encoding

Specifies the character encoding for writing to the file

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

sample code

javascript

const fs = ft.getFileSystemManager()

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

FileSystemManager.writeSync

FileSystemManager.writeSync(Object object)

Synchronous write file

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
fdstringyesFile descriptor that needs to be closed. fd adopt FileSystemManager.open or FileSystemManager.openSync Interface acquisition
datastring/ArrayBufferyesWrite content of type String or ArrayBuffe
offsetnumber0noOnly in data Type is ArrayBuffer When valid, decided arrayBuffe To be written to in the arrayBuffer Index in, default 0
lengthnumbernoOnly in data Type is ArrayBuffer Specifies the number of bytes to write, and defaults to arrayBuffer Offset from 0 offset Number of bytes remaining after 1 byte
encodingstringutf-8noOnly in data Type is String Specifies the character encoding to write to the file, and defaults to utf8
positionnumbernoSpecifies the offset at the beginning of the file, where the data will be written. when position Not passed or passed in Number Type, the data is written to the current pointer location.

object.encoding Legal value

valueIntroductions
base64
binary
utf-8
utf8

Return value

Object res

attributetypeIntroductions
bytesWrittennumberThe actual number of bytes written to the file (note that the number of bytes written is not necessarily the same as the number of characters in the string being written)

sample code

javascript
const fs = ft.getFileSystemManager()

const fd = fs.openSync({
  filePath: `${ft.env.USER_DATA_PATH}/test.txt`,
  flag: 'a+'
})

const res = fs.writeSync({
  fd: fd,
  data: 'this is text'
})
console.log(res)