Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1119671 of FileSystemFileEntry

  • Revision slug: Web/API/FileSystemFileEntry
  • Revision title: FileSystemFileEntry
  • Revision id: 1119671
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("File System API")}}{{Non-standard_header}}

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 methods for creating a {{domxref("FileWriter")}} object to write to the file or a {{domxref("File")}} object to read the file.

Because this is a non-standard API, whose specification is not currently on a standards track, it's important to keep in mind that not all browsers implement it, and those that do may implement only small portions of it. Check the {{anch("Browser compatibility")}} section for details.

Basic concepts

To write content to file, create a {{domxref("FileWriter")}} object by calling {{domxref("FileSystemFileEntry.createWriter", "createWriter()")}}. To read a file, obtain a {{domxref("File")}} oject representing its contents by calling {{domxref("FileSystemFileEntry.file", "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 for 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(); 
     // Note: window.WebKitBlobBuilder.
      bb.append('Meow');
      
      fileWriter.write(bb.getBlob('text/plain'));

    }, errorHandler);

  }, errorHandler);

}

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

Properties

Inherits the properties of its parent interface, {{domxref("FileSystemEntry")}}, but has no properties unique to this interface.

Method overview

void createWriter (FileWriterCallback successCallback, optional ErrorCallback errorCallback);
void file (FileCallback successCallback, optional ErrorCallback errorCallback);

Methods

createWriter()

Creates a new FileWriter object associated with the selected file.

void createWriter (
 in FileWriterCallback successCallback, optional ErrorCallback errorCallback
);
Parameters
successCallback
A callback that is called with the new FileWriter object.
errorCallback
A callback that is called when errors happen.
Returns
void

file()

Returns a File object associated with the selected file. You can use this to read the file's content.

void file (
  FileCallback successCallback, optional ErrorCallback errorCallback
);
Parameters
successCallback
A callback that is called with the new File.
errorCallback
A callback that is called when errors happen.
Returns
void

Specifications

Specification Status Comment
{{SpecName('File System API')}} {{Spec2('File System API')}} Draft of proposed API

This API has no official W3C or WHATWG specification.

Browser compatibility

{{ CompatibilityTable }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 13{{ property_prefix("webkit") }} {{ CompatGeckoDesktop(50) }} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatNo }} 0.16{{ property_prefix("webkit") }} {{CompatGeckoMobile(50)}} {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

See also

Revision Source

<div>{{APIRef("File System API")}}{{Non-standard_header}}</div>

<p>The <strong><code>FileSystemFileEntry</code></strong> interface of the <a href="/en/DOM/File_API/File_System_API" title="en/DOM/File_API/File_System_API">File System API</a> represents a file in a file system. It offers properties describing the file's attributes, as well as methods for creating a {{domxref("FileWriter")}} object to write to the file or a {{domxref("File")}} object to read the file.</p>

<div class="note">
<p>Because this is a non-standard API, whose specification is not currently on a standards track, it's important to keep in mind that not all browsers implement it, and those that do may implement only small portions of it. Check the {{anch("Browser compatibility")}} section for details.</p>
</div>

<h2 id="basic_concepts" name="basic_concepts">Basic concepts</h2>

<p>To write content to file, create a {{domxref("FileWriter")}} object by calling {{domxref("FileSystemFileEntry.createWriter", "createWriter()")}}. To read a file, obtain a {{domxref("File")}} oject representing its contents by calling {{domxref("FileSystemFileEntry.file", "file()")}}.</p>

<h3 id="example" name="example">Example</h3>

<p>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 for <code>error</code> and <code>writeend</code> events. The text data is written to the file by creating a blob, appending text to it, and passing the blob to <code>FileWriter.write()</code>.</p>

<pre class="brush: js">
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(); 
     // Note: window.WebKitBlobBuilder.
      bb.append('Meow');
      
      fileWriter.write(bb.getBlob('text/plain'));

    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);</pre>

<h2 id="Properties" style="line-height: 30px; font-size: 2.14285714285714rem;">Properties</h2>

<p><em>Inherits the properties of its parent interface, {{domxref("FileSystemEntry")}}, but has no properties unique to this interface.</em></p>

<h2 id="Method_overview">Method overview</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>void <a href="#createReader" title="#createWriter">createWriter</a> (FileWriterCallback successCallback, optional ErrorCallback errorCallback);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#File" title="#file">file</a> (FileCallback successCallback, optional ErrorCallback errorCallback)</code>;</td>
  </tr>
 </tbody>
</table>

<h2 id="Methods">Methods</h2>

<h3 id="createWriter" name="createWriter">createWriter()</h3>

<p>Creates a new <code>FileWriter</code> object associated with the selected file.</p>

<pre>
void createWriter (
 in FileWriterCallback successCallback, optional ErrorCallback errorCallback
);</pre>

<h5 id="Parameters">Parameters</h5>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called with the new <code>FileWriter</code> object.</dd>
 <dt>errorCallback</dt>
 <dd>A callback that is called when errors happen.</dd>
</dl>

<h5 id="Returns">Returns</h5>

<dl>
 <dt><code>void</code></dt>
</dl>

<h3 id="File" name="File">file()</h3>

<p>Returns a <a href="/en/docs/Web/API/File"><code>File</code></a> object associated with the selected file. You can use this to read the file's content.</p>

<pre>
void file (
  <em>FileCallback successCallback, optional ErrorCallback errorCallback</em>
);</pre>

<h5 id="Parameters_2">Parameters</h5>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called with the new <code>File</code>.</dd>
 <dt>errorCallback</dt>
 <dd>A callback that is called when errors happen.</dd>
</dl>

<h5 id="Returns_2">Returns</h5>

<dl>
 <dt><code>void</code></dt>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('File System API')}}</td>
   <td>{{Spec2('File System API')}}</td>
   <td>Draft of proposed API</td>
  </tr>
 </tbody>
</table>

<p>This API has no official W3C or WHATWG specification.</p>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>13{{ property_prefix("webkit") }}</td>
   <td>{{ CompatGeckoDesktop(50) }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatNo }}</td>
   <td>0.16{{ property_prefix("webkit") }}</td>
   <td>{{CompatGeckoMobile(50)}}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/File_and_Directory_Entries_API">File and Directory Entries API</a></li>
 <li><a href="/en-US/docs/Web/API/File_and_Directory_Entries_API/Introduction">Introduction to the File System API</a></li>
</ul>
Revert to this revision