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 1125133 of FileSystemFileEntry

  • Revision slug: Web/API/FileSystemFileEntry
  • Revision title: FileSystemFileEntry
  • Revision id: 1125133
  • Created:
  • Creator: Sheppy
  • Is current revision? Yes
  • Comment Edge compat

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.

Properties

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

Methods

{{domxref("FileSystemFileEntry.createWriter", "createWriter()")}}
Creates a new {{domxref("FileWriter")}} object which allows writing to the file represented by the file system entry.
{{domxref("FileSystemFileEntry.file", "file()")}}
Creates a new {{domxref("File")}} object which can be used to read the file.

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")}} object 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();
      bb.append('Meow');
      
      fileWriter.write(bb.getBlob('text/plain'));
    }, errorHandler);

  }, errorHandler);

}

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

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 Microsoft Edge Opera Safari (WebKit)
Basic support 13 {{ property_prefix("webkit") }} {{ CompatGeckoDesktop(50) }} {{ CompatNo }} {{CompatVersionUnknown}}[2] {{ CompatNo }} {{ CompatNo }}
createWriter() 13 {{ property_prefix("webkit") }} {{ CompatNo }}[1] {{ CompatNo }} {{ 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 }}
createWriter() {{ CompatNo }} 0.16{{ property_prefix("webkit") }} {{ CompatNo }}[1] {{ CompatNo }} {{ CompatNo }} {{ CompatNo }}

[1] While the createWriter() method exists in Firefox 50, it is not supported, and immediately reports an NS_ERROR_DOM_SECURITY_ERR error to the error callback.

[2] Microsoft Edge implements this interface as part of the WebKitEntry interface, which is its name for {{domxref("FileSystemEntry")}}.

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="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="Methods">Methods</h2>

<dl>
 <dt>{{domxref("FileSystemFileEntry.createWriter", "createWriter()")}}</dt>
 <dd>Creates a new {{domxref("FileWriter")}} object which allows writing to the file represented by the file system entry.</dd>
 <dt>{{domxref("FileSystemFileEntry.file", "file()")}}</dt>
 <dd>Creates a new {{domxref("File")}} object which can be used to read the file.</dd>
</dl>

<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")}} object 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();
      bb.append('Meow');
      
      fileWriter.write(bb.getBlob('text/plain'));
    }, errorHandler);

  }, errorHandler);

}

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

<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>Microsoft Edge</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>{{CompatVersionUnknown}}<sup>[2]</sup></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
  <tr>
   <td><code>createWriter()</code></td>
   <td>13 {{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo }}<sup>[1]</sup></td>
   <td>{{ CompatNo }}</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>
  <tr>
   <td><code>createWriter()</code></td>
   <td>{{ CompatNo }}</td>
   <td>0.16{{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo }}<sup>[1]</sup></td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] While the <code>createWriter()</code> method exists in Firefox 50, it is not supported, and immediately reports an <code>NS_ERROR_DOM_SECURITY_ERR</code> error to the error callback.</p>

<p>[2] Microsoft Edge implements this interface as part of the <code>WebKitEntry</code> interface, which is its name for {{domxref("FileSystemEntry")}}.</p>

<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