FileSystemFileEntry.file() - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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

FileSystemFileEntry.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 Optional
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 FileError object describing the error.

Editor's note: We need to find out what kinds of errors can occur and document them.

Return value

undefined.

Example

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

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

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

    reader.onerror = function() {
      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 load event handler is set up to deliver the loaded string to the successCallback specified when the readFile() method was called; similarly, its error handler is set up to call the errorCallback specified.

Specifications

SpecificationStatusComment
File and Directory Entries API
The definition of 'file()' in that specification.
DraftInitial specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:94 次

字数:4514

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文