目录

FileSystemManager

File manager app, which can be obtained through has.getFileSystemManager.

Since: 1.0.4

FileSystemManager.open

FileSystemManager.open(Object object): void

Opens a file. This API uses a callback to return a file descriptor.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes File path (local path), which must start with internal://.
flag string r No File system flag. For details about valid values, see FlagMode.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Callback function (success)

The input parameter is an object containing the following fields.

Parameter Type Description
fd string File descriptor.

Error code information

Error Code Error Message
102 unknown flag.
105 open:fail error: invalid path.
106 open:fail error: file already exists.
90002501 open:fail error.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.open({
  filePath: 'internal://cache/test.txt',
  success: (res) => {
    console.info('open success', res);
  },
  fail: (err) => {
    console.error('open fail', err);
  },
  complete: (res) => {
    console.info('open complete', res);
  }
});

FileSystemManager.openSync

FileSystemManager.openSync(Object object): string

Opens a file synchronously. This API uses a callback to return a file descriptor.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes File path (local path), which must start with internal://.
flag string r No File system flag. For details about valid values, see FlagMode.

Returns

Parameter Type Description
fd string File descriptor.

Error code information

Error Code Error Message
105 openSync:fail error: invalid path.
106 openSync:fail error: file already exists.
90002601 openSync:fail error.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});

FileSystemManager.close

FileSystemManager.close(Object object): void

Closes a file.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor to be closed, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102 close fail: The fd is invalid or the fd already closed.
90002701 close:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.close({
  fd: fd,
  success: () => {
    console.info('close success');
  },
  fail: (err) => {
    console.error('close fail', err);
  },
  complete: (res) => {
    console.info('close complete', res);
  }
});

FileSystemManager.closeSync

FileSystemManager.closeSync(Object object): void

Closes a file synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor to be closed, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.

Error code information

Error Code Error Message
102 closeSync fail: The fd is invalid or the fd already closed.
90002801 closeSync:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.closeSync({
  fd: fd
});

FileSystemManager.read

FileSystemManager.read(Object object): void

Reads a file.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
fd string - Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
arrayBuffer ArrayBuffer - Yes Buffer where data is written, which must be an ArrayBuffer instance.
offset number 0 No Write offset in buffer. The default value is 0.
length number 0 No Number of bytes to be read from the file. The default value is 0.
position number - No Start position for reading a file. If this parameter is not specified or is null, the file is read starting from the file pointer position. If the value of position is an integer greater than or equal to 0, the file pointer position remains unchanged and the file is read starting from the specified position.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Callback function (success)

The input parameter is an object containing the following fields.

Parameter Type Description
bytesRead number Number of bytes read.
arrayBuffer ArrayBuffer Buffer where data is written.

Error code information

Error Code Error Message
102

1. read fail: The fd is invalid or the fd already closed.

2. read fail: The offset must be >=0.

3. read fail: The length must be >=0.

4. read fail: The position must be >=0.

90001801 read failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.read({
  fd: fd,
  arrayBuffer: new ArrayBuffer(10),
  length: 10,
  success: (res) => {
    console.info('read success', res.bytesRead);
  },
  fail: (err) => {
    console.error('read fail', err);
  },
  complete: (res) => {
    console.info('read complete', res);
  }
});

FileSystemManager.readSync

FileSystemManager.readSync(Object object): ReadResult

Reads a file synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
fd string - Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
arrayBuffer ArrayBuffer - Yes Buffer where data is written, which must be an arrayBuffer instance.
offset number 0 No

Write offset in buffer, in bytes.

Recommended value range: [0, length of arrayBuffer]. If the value is less than 0, 0 is used. If the value is greater than the length of arrayBuffer, the length of arrayBuffer is used.

length number 0 No Number of bytes to be read from the file, in bytes.
position number - No Start position for reading a file. If this parameter is not specified or is null, the file is read starting from the file pointer position. If the value of position is an integer greater than or equal to 0, the file pointer position remains unchanged and the file is read starting from the specified position.

Returns

ReadResult, which is an object containing the following fields.

Parameter Type Description
bytesRead number Number of bytes read.
arrayBuffer ArrayBuffer Buffer where data is written.

Error code information

Error Code Error Message
102

1. readSync fail: The fd is invalid or the fd already closed.

2. readSync fail: The offset must be >=0.

3. readSync fail: The length must be >=0.

4. readSync fail: The position must be >=0.

90001901 readSync:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
const readResult = fileSystemManager.readSync({
  fd: fd,
  arrayBuffer: new ArrayBuffer(10),
  length: 10
});
console.info('bytesRead', readResult.bytesRead);
console.info('arrayBuffer', readResult.arrayBuffer);

FileSystemManager.fstat

FileSystemManager.fstat(Object object): void

Obtains file status information.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Callback function (success)

The input parameter is an object containing the following fields.

Parameter Type Description
stats Stats Stats object, which contains the file status information.

Error code information

Error Code Error Message
102 fstat fail: The fd is invalid or the fd already closed.
90003401 fstat error:

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.fstat({
  fd: fd,
  success: (res) => {
    console.info('fstat success', res);
    console.info('fstat success', res.stats.isFile());
  },
  fail: (err) => {
    console.error('fstat fail', err);
  },
  complete: (res) => {
    console.info('fstat complete', res);
  }
});

FileSystemManager.fstatSync

FileSystemManager.fstatSync(Object object): Stats

Obtains file status information synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.

Returns

Parameter Type Description
stats Stats Stats object, which contains the file status information.

Error code information

Error Code Error Message
102 fstatSync fail: The fd is invalid or the fd already closed.
90003501 fstatSync error.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  const fd = fileSystemManager.openSync({
    filePath: 'internal://cache/test.txt'
  });
  const stats = fileSystemManager.fstatSync({
    fd: fd
  });
  console.info('fstatSync result', stats);
  console.info('fstatSync result', stats.isFile());
} catch (err) {
  console.error('fstatSync error', err);
}

FileSystemManager.ftruncate

FileSystemManager.ftruncate(Object object): void

Truncates the file content.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
length number Yes

Position where the content is truncated, in bytes.

If the value of length is smaller than the file length (in bytes), only the first length bytes are retained in the file, and the rest of the content is truncated. If the value of length is greater than the file length, the file is extended and the extended part is filled with empty bytes ('\0').

success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102

1. ftruncate:fail the length must be greater than 0.

2. ftruncate fail: The fd is invalid or the fd already closed.

108 ftruncate:fail the filePath is not writeable.
90003101 ftruncate:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt', // The file needs to be written.
  flag: 'r+'
});
fileSystemManager.ftruncate({
  fd: fd,
  length: 0,
  success: () => {
    console.info('ftruncate success');
  },
  fail: (err) => {
    console.error('ftruncate fail', err);
  },
  complete: (res) => {
    console.info('ftruncate complete', res);
  }
});

FileSystemManager.ftruncateSync

FileSystemManager.ftruncateSync(Object object): void

Truncates the file content synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
fd string Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
length number Yes

Position where the content is truncated, in bytes.

If the value of length is smaller than the file length (in bytes), only the first length bytes are retained in the file, and the rest of the content is truncated. If the value of length is greater than the file length, the file is extended and the extended part is filled with empty bytes ('\0').

Error code information

Error Code Error Message
102

1. ftruncateSync fail: The fd is invalid or the fd already closed.

2. ftruncateSync:fail the length must be greater than 0.

105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 ftruncateSync:fail the filePath is not writeable.
90003201 ftruncateSync:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt', // The file needs to be written.
  flag: 'r+'
});
fileSystemManager.ftruncateSync({
  fd: fd,
  length: 0
});

FileSystemManager.truncate

FileSystemManager.truncate(Object object): void

Truncates the file content.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the file to be truncated (local path), which must start with internal://.
length number 0 No

Position where the content is truncated, in bytes.

If the value of length is smaller than the file length (in bytes), only the first length bytes are retained in the file, and the rest of the content is truncated. If the value of length is greater than the file length, the file is extended and the extended part is filled with empty bytes ('\0').

Recommended value range: [0, file length].

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102 truncate:fail the length must be greater than 0.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 truncate:fail the filePath is not writeable.
90002901 truncate failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.truncate({
  filePath: 'internal://cache/test.txt',
  length: 0,
  success: () => {
    console.info('truncate success');
  },
  fail: (err) => {
    console.error('truncate fail', err);
  },
  complete: (res) => {
    console.info('truncate complete', res);
  }
});

FileSystemManager.truncateSync

FileSystemManager.truncateSync(Object object): void

Truncates the file content synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the file to be truncated (local path).
length number 0 No Position where the content is truncated. If the value of length is smaller than the file length (in bytes), only the first length bytes are retained in the file, and the rest of the content is truncated. If the value of length is greater than the file length, the file is extended and the extended part is filled with empty bytes ('\0').

Error code information

Error Code Error Message
102 truncate:fail the length must be greater than 0.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 truncate:fail the filePath is not writeable.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.truncateSync({
  filePath: 'internal://cache/test.txt',
  length: 0
});

FileSystemManager.write

FileSystemManager.write(Object object): void

Writes a file.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
fd string - Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
data string | arrayBuffer - Yes Content to be written, which is of the string or arrayBuffer type.
offset number 0 No Position in arrayBuffer where data is to be written, that is, the index in arrayBuffer. This parameter is valid only when data is of the arrayBuffer type.
length number 0 No Number of bytes to write. This parameter is valid only when data is of the arrayBuffer type. The default value is the number of bytes remaining after the bytes are offset from 0 in arrayBuffer.
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

position number - No Position in the file where data is to be written. If position is not specified or is set to null, data is written to the position where the pointer is located.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Callback function (success)

The input parameter is an object containing the following fields.

Parameter Type Description
bytesWritten number Number of bytes written to the file. (Note that the number of written bytes may be different from the number of written characters.)

Error code information

Error Code Error Message
102

1. write:fail invalid fd.

2. write:fail.

90000701 write:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt',
  flag: 'r+'
});
fileSystemManager.write({
  fd: fd,
  data: 'some text',
  success: (res) => {
    console.info('write success', res.bytesWritten);
  },
  fail: (err) => {
    console.error('write fail', err);
  },
  complete: (res) => {
    console.info('write complete', res);
  }
});

FileSystemManager.writeSync

FileSystemManager.writeSync(Object object): WriteResult

Writes data to a file synchronously.

Since: 1.0.16

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
fd string - Yes File descriptor, which is obtained through the FileSystemManager.open or FileSystemManager.openSync API.
data string | arrayBuffer - Yes Content to be written, which is of the string or arrayBuffer type.
offset number 0 No Position in arrayBuffer where data is to be written, that is, the index in arrayBuffer. This parameter is valid only when data is of the arrayBuffer type.
length number 0 No Number of bytes to write. This parameter is valid only when data is of the arrayBuffer type. The default value is the number of bytes remaining after the bytes are offset from 0 in arrayBuffer.
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

position number - No Position in the file where data is to be written. If position is not specified or is set to null, data is written to the position where the pointer is located.

Returns

WriteResult, which is an object containing the following fields.

Parameter Type Description
bytesWritten number Number of bytes written to the file. (Note that the number of written bytes may be different from the number of written characters.)

Error code information

Error Code Error Message
102 writeSync fail: The fd is invalid or the fd already closed.
90000801 writeSync:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt',
  flag: 'r+'
});
const writeResult = fileSystemManager.writeSync({
  fd: fd,
  data: 'some text'
});
console.info('writeSync.bytesWritten ', writeResult.bytesWritten);

FileSystemManager.access

FileSystemManager.access(Object object)

Checks whether a file or directory exists. This API uses an asynchronous callback to return the result.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
path string Yes Path of the file or directory to check. The value can be a path starting with internal:// or an absolute path of a resource file.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of fail

Returns the following if the file or directory does not exist:

{errMsg: 'access:fail no such file or directory.'}

Error code information

Error Code Error Message
101

1. access:error.

2. access:fail, message.

3. context is undefined.

104 access:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.access({
  path: 'internal://cache/demo.png',
  success: () => {
    console.info('access success');
  },
  fail: (err) => {
    console.error('access fail', err);
  },
  complete: (res) => {
    console.info('access complete', res);
  }
});

FileSystemManager.accessSync

FileSystemManager.accessSync(string path): void

Checks whether a file or directory exists. This API returns the result synchronously. If the file or directory exists, the execution continues. Otherwise, an exception will be thrown.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
path string Yes Path of the file or directory to check. The value can be a path starting with internal:// or an absolute path of a resource file.

Error code information

Error Code Error Message
101 accessSync:error.
104 ccessSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.accessSync('internal://cache/demo.png');
} catch (err) {
  console.error('accessSync error', err);
}

FileSystemManager.appendFile

FileSystemManager.appendFile(Object object)

Appends content to the end of a file.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the target file, which must start with internal://.
data string | ArrayBuffer - Yes Text or binary data to append. (The support for the ArrayBuffer type depends on the framework.)
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
102 appendFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 appendFile:fail the filePath is not writeable.
90000101 appendFile:fail can not write data to the file.
90000102 appendFile:fail can not open file. It may not be a valid file.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.appendFile({
  filePath: 'internal://cache/demo.png',
  data: 'Content to append',
  success: () => {
    console.info('appendFile success');
  },
  fail: (err) => {
    console.error('appendFile fail', err);
  },
  complete: (res) => {
    console.info('appendFile complete', res);
  }
});

FileSystemManager.appendFileSync

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

Appends content to the end of a file. If the operation is successful, the execution continues. Otherwise, an exception will be thrown. (The support for the ArrayBuffer type depends on the framework.)

Since: 1.0.4

Parameters

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the target file, which must start with internal://.
data string | ArrayBuffer - Yes Text or binary data to append.
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

Error code information

Error Code Error Message
102 appendFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 appendFile:fail the filePath is not writeable.
90000202 appendFile:fail can not open file. It may not be a valid file.
90000201 appendFile:fail can not write data to the file.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.appendFileSync('internal://cache/demo.png', 'Data to append', 'utf-8');
} catch (err) {
  console.error('appendFileSync error', err);
}

FileSystemManager.copyFile

FileSystemManager.copyFile(Object object)

Copies a file. This API uses an asynchronous callback to return the result.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
srcPath string Yes

Source file path. The following two types of paths are supported:

- file:// protocol path, which is usually used for files selected from the file selector.

- internal:// protocol path.

destPath string Yes Target file path, which must start with internal://files/ or internal://cache/.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of fail

{ errMsg: 'copyFile:fail  reason' }

Error code information

Error Code Error Message
102 permission denied, invalid picker path.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 copyFile:fail the filePath is not writeable.
90001101 copyFile:fail to copy the file.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.copyFile({
  srcPath: 'internal://cache/demo.png',
  destPath: 'internal://cache/demo_copy.png',
  success: () => {
    console.info('copyFile success');
  },
  fail: (err) => {
    console.error('copyFile fail', err);
  },
  complete: (res) => {
    console.info('copyFile complete', res);
  }
});

FileSystemManager.copyFileSync

FileSystemManager.copyFileSync(string srcPath, string destPath): void

Copies a file. This API returns the result synchronously. If the operation is successful, the execution continues. Otherwise, an exception will be thrown.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
srcPath string Yes

Source file path. The following two types of paths are supported:

- file:// protocol path, which is usually used for files selected from the file selector.

- internal:// protocol path.

destPath string Yes Target file path, which must start with internal://files/ or internal://cache/.

Error code information

Error Code Error Message
102 permission denied, invalid picker path.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 copyFileSync:fail the filePath is not writeable.
90001201 copyFileSync:fail to copy the file.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.copyFileSync('internal://cache/demo.png', 'internal://cache/demo_copy.png');
} catch (err) {
  console.error('copyFileSync error', err);
}

FileSystemManager.getFileInfo

FileSystemManager.getFileInfo(Object object)

Obtains information about a local temporary file or cached file. This API uses an asynchronous callback to return the result.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the target file, which must start with internal://.
digestAlgorithm string md5 No

Algorithm for calculating the file digest. Available values are as follows:

- md5

- sha1

- sha256

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
size number File size, in bytes.
digest string File digest, which is calculated based on the input digestAlgorithm.

Error code information

Error Code Error Message
101 getFileInfo:fail.
102 getFileInfo:fail invalid digestAlgorithm.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.getFileInfo({
  filePath: 'internal://cache/demo.png',
  digestAlgorithm: 'sha256',
  success: (res) => {
    console.info('getFileInfo success', res);
  },
  fail: (err) => {
    console.error('getFileInfo fail', err);
  },
  complete: (res) => {
    console.info('getFileInfo complete', res);
  }
});

FileSystemManager.getSavedFileList

FileSystemManager.getSavedFileList(Object object)

Obtains a list of files in internal://files/store/. You should save the cached files to this path. This API uses an asynchronous callback to return the result.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
fileList Array<object> Array of files. Each item is a file.

fileList

Attribute Type Description
filePath string Local file path.
size number Size of a local file, in bytes.
createTime number Timestamp when the file was saved the last time. The value is the number of seconds elapsed since 1970/07/07 08:00:00 GMT.

Error code information

Error Code Error Message
101 getSavedFileList:fail.
90001301 getSavedFileList:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.getSavedFileList({
  success: (res) => {
    console.info('getSavedFileList success', res);
  },
  fail: (err) => {
    console.error('getSavedFileList fail', err);
  },
  complete: (res) => {
    console.info('getSavedFileList complete', res);
  }
});

FileSystemManager.mkdir

FileSystemManager.mkdir(Object object)

Creates a directory.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
dirPath string - Yes Path of the directory to create, which must start with internal://files/ or internal://cache/.
recursive boolean false No Whether to create parent directories recursively if they do not exist.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 fail, recursive is false and upper path does not exist.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

106 dirPath already exists.
90000301 mkdir failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.mkdir({
  dirPath: 'internal://files/example',
  recursive: false,
  success: () => {
    console.info('mkdir success');
  },
  fail: (err) => {
    console.error('mkdir fail', err);
  },
  complete: (res) => {
    console.info('mkdir complete', res);
  }
});

FileSystemManager.mkdirSync

FileSystemManager.mkdirSync(string dirPath, boolean recursive): void

Creates a directory. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Default Value Mandatory Description
dirPath string - Yes Path of the directory to create, which must start with internal://files/ or internal://cache/.
recursive boolean false No Whether to create parent directories recursively if they do not exist.

Error code information

Error Code Error Message
101 fail, recursive is false and upper path does not exist.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

106 dirPath already exists.
90000401 mkdirSync.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.mkdirSync('internal://files/example', false);
} catch (err) {
  console.error('mkdirSync error', err);
}

FileSystemManager.readdir

FileSystemManager.readdir(Object object)

Reads the file list of a directory.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
dirPath string Yes Path (local path) of the target directory.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
files Array<string> Array of file names in a specified directory.

Error code information

Error Code Error Message
104 readdir:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. readdir:fail not a directory.

6. readdir:fail parameter error: Invalid filePath.

90000901

1. readdir:error.

2. readdir failed with error message.

For more information, see Basic File IO Error Codes.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.readdir({
  dirPath: 'internal://files/example',
  success: (res) => {
    console.info('readdir success', res);
  },
  fail: (err) => {
    console.error('readdir fail', err);
  },
  complete: (res) => {
    console.info('readdir complete', res);
  }
});

FileSystemManager.readdirSync

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

Reads the file list of a directory. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
dirPath string Yes Path (local path) of the target directory.

Returns

Type Description
Array<string> Array of file names in a specified directory.

Error code information

Error Code Error Message
104 readdirSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. readdirSync:fail not a directory.

6. readdirSync:fail parameter error: Invalid filePath.

90001001

1. readdirSync: error.

2. readdirSync failed with error message:

For more information, see Basic File IO Error Codes.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.readdirSync('internal://files/example');
  console.info('readdirSync success', res);
} catch (err) {
  console.error('readdirSync error', err);
}

FileSystemManager.readFile

FileSystemManager.readFile(Object object)

Reads the content of a local file.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
filePath string Yes Path of the file to read, which must start with internal://.
encoding string No

Encoding format of the content read from the file. If this parameter is not specified, the binary content is returned in ArrayBuffer format.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

position number No Start position to read, in bytes. If this parameter is not specified, the content is read from the beginning of the file. Content read range: [position, position + length) Value range: [0, fileLength – 1]. Unit: byte.
length number No Length of the content to read, in bytes. If this parameter is not specified, it will read until the end of the file. Value range: [1, fileLength]. Unit: byte.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
data string | ArrayBuffer File content.

Error code information

Error Code Error Message
102

1. fail the encoding parameter is invalid.

2. fail no such file or directory.

3. fail value of length is out of range.

4. fail value of position is out of range.

104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail not a file.

90001801 readFile failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.readFile({
  filePath: 'internal://files/hello.txt',
  encoding: 'utf-8',
  position: 0,
  success: (res) => {
    console.info('readFile success', res);
  },
  fail: (err) => {
    console.error('readFile fail', err);
  },
  complete: (res) => {
    console.info('readFile complete', res);
  }
});

FileSystemManager.readFileSync

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

Reads the content of a local file. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
filePath string Yes Path (local path) of the file to read, which must start with internal://.
encoding string No

Encoding format of the content read from the file. If this parameter is not specified, the binary content is returned in ArrayBuffer format.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

position number No Start position to read, in bytes. If this parameter is not specified, the content is read from the beginning of the file. Content read range: [position, position + length). Value range: [0, fileLength – 1]. Unit: byte.
length number No Length of the content to read, in bytes. If this parameter is not specified, it will read until the end of the file. Value range: [1, fileLength]. Unit: byte.

Returns

Attribute Type Description
data string | ArrayBuffer File content.

Error code information

Error Code Error Message
102

1. fail the encoding parameter is invalid.

2. fail value of length is out of range.

3. fail value of position is out of range.

104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail not a file.

90001901 readFileSync failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.readFileSync('internal://files/hello.txt', 'utf-8', 0);
  console.info('readFileSync success', res);
} catch (err) {
  console.error('readFileSync error', err);
}

FileSystemManager.removeSavedFile

FileSystemManager.removeSavedFile(Object object)

Removes the local cached files of this atomic service.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
filePath string Yes Path of the files to delete, which must be in internal://files/store/.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail file is directory.

104 fail no such file or directory.
90001701 removeSavedFile: fail.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.removeSavedFile({
  filePath: 'internal://files/store/hello.txt',
  success: () => {
    console.info('removeSavedFile success');
  },
  fail: (err) => {
    console.error('removeSavedFile fail', err);
  },
  complete: (res) => {
    console.info('removeSavedFile complete', res);
  }
});

FileSystemManager.rename

FileSystemManager.rename(Object object)

Renames a file. You can also use this API to move a file to another directory.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
oldPath string Yes Source file path, which supports a local path.
newPath string Yes New file path, which must start with internal://files/ or internal://cache/.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 rename:fail.
104 rename:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 rename:fail the newPath is not writeable.
90001501 rename:fail error message.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.rename({
  oldPath: 'internal://files/hello.txt',
  newPath: 'internal://files/hello_new.txt',
  success: () => {
    console.info('rename success');
  },
  fail: (err) => {
    console.error('rename fail', err);
  },
  complete: (res) => {
    console.info('rename complete', res);
  }
});

FileSystemManager.renameSync

FileSystemManager.renameSync(string oldPath, string newPath): void

Renames a file. This API returns the result synchronously. You can also use this API to move a file to another directory.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
oldPath string Yes Source file path, which supports a local path.
newPath string Yes New file path, which supports a local path.

Error code information

Error Code Error Message
101 renameSync:fail.
104 renameSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 rename:fail the newPath is not writeable.
90001601 renameSync:fail error message.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.renameSync('internal://files/hello.txt', 'internal://files/hello_new.txt');
} catch(err) {
  console.error('renameSync error', err);
}

FileSystemManager.rmdir

FileSystemManager.rmdir(Object object)

Removes a directory.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
dirPath string - Yes Path of the directory to remove, which must start with internal://files/ or internal://cache/.
recursive boolean false No Whether to remove subdirectories and files within the directory recursively.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
104 fail no such directory.
105 fail parameter error: Invalid path.
107 fail directory not empty.
90000501 rmdir failed with error message.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.rmdir({
  dirPath: 'internal://files/example',
  recursive: false,
  success: () => {
    console.info('rmdir success');
  },
  fail: (err) => {
    console.error('rmdir fail', err);
  },
  complete: (res) => {
    console.info('rmdir complete', res);
  }
});

FileSystemManager.rmdirSync

FileSystemManager.rmdirSync(string dirPath, boolean recursive): void

Removes a directory. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Default Value Mandatory Description
dirPath string - Yes Path of the directory to remove, which must start with internal://files/ or internal://cache/.
recursive boolean false No Whether to remove subdirectories and files within the directory recursively.

Error code information

Error Code Error Message
104 fail no such directory.
105 fail parameter error: Invalid path.
107 fail directory not empty.
90000601 rmdirSync.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.rmdirSync('internal://files/example', false);
} catch (err) {
  console.error('rmdirSync error', err);
}

FileSystemManager.saveFile

FileSystemManager.saveFile(Object object)

Saves a temporary file to a local path. This API moves the temporary file from tempFilePath (internal://tmp/) to filePath (internal://files/store/). After the API is successfully called, tempFilePath will be unavailable.

filePath can be set to a path in the store directory (internal://files/store/) only when tempFilePath is a path in the tmp directory (internal://tmp/).

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
tempFilePath string Yes Path of the temporary file to save, which must start with internal://.
filePath string No Path of the destination directory to save the temporary file, which must start with internal://files/ or internal://cache/.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
savedFilePath string Path (local path) of the file saved.

Error code information

Error Code Error Message
104 saveFile:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 saveFile:fail the filePath is not writeable.
90001401 saveFile:fail init store path fail.
90001402 saveFile: directory is not supported.
90001403 saveFile: Failed to copy.
90001404 saveFile:fail illegal Operation on a directory.
90001405 saveFile:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.saveFile({
  tempFilePath: 'internal://tmp/templete.png',
  success: (res) => {
    console.info('saveFile success', res);
  },
  fail: (err) => {
    console.error('saveFile fail', err);
  },
  complete: (res) => {
    console.info('saveFile complete', res);
  }
});

FileSystemManager.saveFileSync

FileSystemManager.saveFileSync(string tempFilePath, string filePath): string

Saves a temporary file to a local directory permanently. This API returns the result synchronously.

filePath can be set to a path in the store directory (internal://files/store/) only when tempFilePath is a path in the tmp directory (internal://tmp/).

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
tempFilePath string Yes Path of the temporary file to save, which must start with internal://.
filePath string No Path of the destination directory to save the temporary file, which must start with internal://files/ or internal://cache/.

Returns

Attribute Type Description
savedFilePath string Path (local path) of the file saved.

Error code information

Error Code Error Message
101 saveFileSync:fail.
104 saveFileSync:fail no such file.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 saveFileSync:fail the filePath is not writeable.
90001401 saveFileSync:fail init store path fail.

Example:

const fileSystemManager = has.getFileSystemManager();
const savedFilePath = fileSystemManager.saveFileSync('internal://tmp/demo.png');

FileSystemManager.stat

FileSystemManager.stat(Object object)

Obtains the Stats object of a file.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
path string - Yes Path of the file or directory, which must start with internal://.
recursive boolean false No Whether to recursively obtain the Stats information of each file in the directory.
success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Attribute Type Description
stats Stats|Array<FileStats>

When recursive is set to false, res.stats is a Stats object.

When recursive is set to true and path is a directory path, res.stats is an array. Each item in the array is an object, and each object contains path and stats.

Error code information

Error Code Error Message
105

stat:fail parameter error: Invalid path.

For more information, see Basic File IO Error Codes.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.stat({
  path: 'internal://cache/demo.png',
  recursive: false,
  success: (res) => {
    console.info('stat success', res);
  },
  fail: (err) => {
    console.error('stat fail', err);
  },
  complete: (res) => {
    console.info('stat complete', res);
  }
});

FileSystemManager.statSync

FileSystemManager.statSync(string path, boolean recursive): Object

Obtains the Stats object of a file. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Default Value Mandatory Description
path string - Yes Path of the file or directory, which must start with internal://.
recursive boolean false No Whether to recursively obtain the Stats information of each file in the directory.

Returns

An object containing the following fields.

Attribute Type Description
stats Stats|Array<FileStats>

When recursive is set to false, stats is a Stats object.

When recursive is set to true and path is a directory path, stats is an array. Each item in the array is an object, and each object contains path and stats.

Error code information

Error Code Error Message
105

statSync:fail parameter error: Invalid path.

For more information, see Basic File IO Error Codes.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.statSync('internal://cache/demo.png');
  console.info('statSync result', res);
} catch (err) {
  console.error('statSync error', err);
}

FileSystemManager.unlinkSync

FileSystemManager.unlinkSync(string filePath): void

Deletes a file. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Mandatory Description
filePath string Yes Path of the target file, which must start with internal://.

Error code information

Error Code Error Message
104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail file is directory.

90002301 unlinkSync:fail.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.unlinkSync('internal://cache/demo.png');

FileSystemManager.unzip

FileSystemManager.unzip(Object object)

Unzips a file.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
zipFilePath string Yes Path of the ZIP file to unzip, which must start with internal://.
targetPath string Yes Destination directory for the unzipped file, which must start with internal://files/ or internal://cache/.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
104 unzip:fail zipFilePath no such file or directory.
105

1. unzip:fail parameter error: Invalid zipFilePath.

2. fail parameter error: path is empty.

3. fail parameter error: parameter.path should be String.

4. fail parameter error: Invalid path.

5. fail parameter error: path is too long.

6. unzip:fail parameter error: Invalid targetPath.

7. unzip:fail zipFilePath not a file.

8. unzip:fail targetPath not a directory.

For more information, see Basic File IO Error Codes.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.unzip({
  zipFilePath: 'internal://cache/example.zip',
  targetPath: 'internal://cache/example',
  success: () => {
    console.info('unzip success');
  },
  fail: (err) => {
    console.error('unzip fail', err);
  },
  complete: (res) => {
    console.info('unzip complete', res);
  }
});

FileSystemManager.writeFile

FileSystemManager.writeFile(Object object)

Writes a file.

Since: 1.0.4

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the target file, which must start with internal://files/ or internal://cache/.
data string | ArrayBuffer - Yes Text or binary data to write.
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

success function - No Callback triggered when the API call is successful.
fail function - No Callback triggered when the API call fails.
complete function - No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
101 writeFile:fail.
102 writeFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108

1. writeFile:fail the filePath is not writeable.

2. writeFile:fail the filePath is invalid.

90000701 writeFile:fail can not write data to the file.
90000702 writeFile:fail can not open file. It may not be a valid file.

Example:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.writeFile({
  filePath: 'internal://cache/demo.txt',
  data: 'Text to write',
  encoding: 'utf-8',
  success: () => {
    console.info('writeFile success');
  },
  fail: (err) => {
    console.error('writeFile fail', err);
  },
  complete: (res) => {
    console.info('writeFile complete', res);
  }
});

FileSystemManager.writeFileSync

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

Writes a file. This API returns the result synchronously.

Since: 1.0.4

Parameters

Parameter Type Default Value Mandatory Description
filePath string - Yes Path of the target file, which must start with internal://files/ or internal://cache/.
data string | ArrayBuffer - Yes Text or binary data to write.
encoding string utf-8 No

Character encoding of the file to be written. This parameter is valid only when data is of the string type.

In versions earlier than ASCF 2.0.5, only utf-8 is supported.

In ASCF 2.0.5 and later versions, the following encodings are supported: ascii, base64, binary, hex, ucs2(ucs-2), utf16le(utf-16le), utf-8(utf8), and latin1.

Error code information

Error Code Error Message
101 writeFileSync:fail.
102 writeFileSync:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String/

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108

1. writeFileSync:fail the filePath is not writeable.

2. writeFileSync:fail the filePath is invalid.

90000801 writeFileSync:fail can not write data to the file.
90000802 writeFileSync:fail can not open file. It may not be a valid file.

Example:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.writeFileSync('internal://cache/demo.txt', 'Text to write', 'utf-8');
} catch (err) {
  console.error('writeFileSync error', err);
}