大网数据广告活动
Web 标准与前端开发

FileSystemFileEntry: file() method

· 52CE 我爱测

The FileSystemFileEntry interface's method **`file()`** returns a File object which can be used to read data from the file represented by the directory entry.

File and Directory Entries API

The FileSystemFileEntry interface's method file() returns a File object which can be used to read data from the file represented by the directory entry.

Syntax

file(successCallback)
file(successCallback, errorCallback)

Parameters

  • successCallback
    • : A callback function which is called when the File has been created successfully; the File is passed into the callback as the only parameter.
  • errorCallback
    • : If provided, this must be a method which is called when an error occurs while trying to create the File. This callback receives as input a DOMException object describing the error.

Return value

None (undefined).

Examples

This example establishes a method, readFile(), reads a text file and calls a specified callback function with the received text (in a string) once the read is completed. If an error occurs, a specified (optional) error callback is called.

function readFile(entry, successCallback, errorCallback) {
  entry.file((file) => {
    let reader = new FileReader();

    reader.onload = () => {
      successCallback(reader.result);
    };

    reader.onerror = () => {
      errorCallback(reader.error);
    };

    reader.readAsText(file);
  }, errorCallback);
}

This function calls file(), specifying as its success callback a method which proceeds to use a FileReader to read the file as text. The FileReader's FileReader/load_event event handler is set up to deliver the loaded string to the successCallback specified when the readFile() method was called; similarly, its FileReader/error_event handler is set up to call the errorCallback specified.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。