FileSystemFileEntry - Web APIs 编辑

Experimental

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

The FileSystemFileEntry interface of the File System API represents a file in a file system. It offers properties describing the file's attributes, as well as the file() method, which creates a File object that can be used to read the file.

Properties

Inherits the properties of its parent interface, FileSystemEntry, but has no properties unique to this interface.

Methods

file()
Creates a new File object which can be used to read the file.

Obsolete methods

createWriter() This is an obsolete API and is no longer guaranteed to work.
Creates a new FileWriter object which allows writing to the file represented by the file system entry.

Basic concepts

To write content to file, create a FileWriter object by calling createWriter(). To read a file, obtain a File object representing its contents by calling file().

Example

The following code creates an empty file called "log.txt" (if it doesn't exist) and fills it with the text "Meow". Inside the success callback, event handlers are set up to handle the error error and writeend events. The text data is written to the file by creating a blob, appending text to it, and passing the blob to FileWriter.write().

function onInitFs(fs) {
  fs.root.getFile('log.txt', {create: true}, function(fileEntry) {

    // Create a FileWriter object for our FileSystemFileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {
      fileWriter.onwriteend = function(e) {
        console.log('Write completed.');
      };

      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var bb = new BlobBuilder();
      bb.append('Meow');

      fileWriter.write(bb.getBlob('text/plain'));
    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);

Specifications

SpecificationStatusComment
File and Directory Entries API
The definition of 'FileSystemFileEntry' in that specification.
DraftDraft of proposed API

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:130 次

字数:6310

最后编辑:7 年前

编辑次数:0 次

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