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 1119653 of FileSystemEntry

  • Revision slug: Web/API/FileSystemEntry
  • Revision title: FileSystemEntry
  • Revision id: 1119653
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Error callback gets a DOMError in Firefox

Revision Content

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

The FileSystemEntry interface of the File and Directory Entries API represents a single in a file system. The entry can be a file or a directory (directories are represented by the {{domxref("DirectoryEntry")}} interface. It includes methods for working with files—including copying, moving, removing, and reading files—as well as information about a file it points to—including the file name and its path from the root to the entry.

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

You don't create FileSystemEntry objects directly. Instead, you will receive an object based on this interface through other APIs. This interface serves as a base class for the {{domxref("FileSystemFileEntry")}} and {{domxref("FileSystemDirectoryEntry")}} interfaces, which provide features specific to file system entries representing files and directories, respectively.

The FileSystemEntry interface includes methods that you would expect for manipulating files and directories, but it also includes a convenient method for obtaining the URL of the entry: toURL(). It also introduces a new URL scheme: filesystem:.

You can use the filesystem: scheme on Google Chrome to see all the files and folders that are stored in the origin of your app. Just use filesystem: scheme for the root directory of the origin of the app. For example, if your app is in https://www.html5rocks.com, open filesystem:https://www.html5rocks.com/temporary/ in a tab. Chrome shows a read-only list of all the files and folders stored the origin of your app.

Example

To see an example of how toURL() works, see the method description. The snippet below shows you how you can remove a file by name.

// Taking care of the browser-specific prefixes.
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem; 

...

// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, function(fs) {
  fs.root.getFile('log.txt', {}, function(fileEntry) {
    
    fileEntry.remove(function() {
      console.log('File removed.');
    }, onError);
  
  }, onError);
}, onError); 

Method overview

void getMetadata (in MetadataCallback successCallback, in optional ErrorCallback errorCallback);
void moveTo (in DirectoryEntry parent, optional DOMString newName, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback);
void copyTo (in DirectoryEntry parent, in optional DOMString newName, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback);
void toURL (in optional DOMString mimeType);
void remove (in VoidCallback successCallback, in optional ErrorCallback errorCallback);
void getParent (in EntryCallback successCallback, in optional ErrorCallback errorCallback);

Properties

This interface provides the following properties.

filesystem {{ReadOnlyInline}}
A {{domxref("FileSystem")}} object representing the file system in which the entry is located.
fullPath {{ReadOnlyInline}}
A {{domxref("DOMString")}} object which provides the full, absolute path from the file system's root to the entry; it can also be thought of as a path which is relative to the root directory, prepended with a "/" character.
isDirectory {{ReadOnlyInline}}
A {{jsxref("Boolean")}} which is true if the entry represents a directory; otherwise, it's false.
isFile {{ReadOnlyInline}}
A Boolean which is true if the entry represents a file. If it's not a file, this value is false.
name {{ReadOnlyInline}}
A {{domxref("DOMString")}} containing the name of the entry (the final part of the path, after the last "/" character).

Methods

getMetadata()

Look up last modification date of the entry.

void getMetadata (
  in MetadataCallback ErrorCallback
);
Parameter
successCallback
A callback that is called with the time of the last modification.
errorCallback
A callback that is called when errors happen.
Returns
void

moveTo()

Move an entry to a different location on the file system. Moving a file over an existing file replaces that existing file. A move of a directory on top of an existing empty directory replaces that directory.

You can also use this method for renaming files. You can keep it in the same location and then define the newName parameter.

You cannot do the following:

  • Move a directory inside itself or to any child at any depth
  • Move an entry into its parent if a name different from its current one isn't provided
  • Move a file to a path occupied by a directory or move a directory to a path occupied by a file
  • Move any element to a path occupied by a directory that is not empty.
void moveTo (
  in DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback
);
Parameter
parent
The directory to which to move the entry.
newName
The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.
successCallback
A callback that is called when the file or directory has been successfully moved.
errorCallback
An optional callback that is called only when errors happen.
Returns
void

copyTo()

Copy an entry to a different location on the file system. You cannot copy an entry inside itself if it is a directory, nor can you copy it into its parent if you don't provide a new name. Directory copies are always recursive—that is, they copy all contents of the directory. You cannot change this behavior. Files are simply duplicated.

void copyTo (
  in DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback
);
Parameter
parent
The directory where you want the entry to move to.
newName
The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.
successCallback
A callback that is called when the file or directory has been successfully copied.
errorCallback
An optional callback that is called only when errors happen.
Returns
void

toURL()

Returns a URL that can be used to identify this entry. It exposes a new URL scheme—filesystem:—that you can use to fill src or href attributes. For example, if you wanted to display an image and have its fileEntry, calling toURL() gives you the image file's file system URL. You get something like: filesystem:https://example.com/temporary/lolcat.png.

The following is an example:

var img = document.createElement('img'); 
// Get the URL of the entry, which returns: filesystem:https://example.com/temporary/lolcat.png 
img.src = fileEntry.toURL(); 
document.body.appendChild(img);

The file system URL does not expire. Because the method describes a location on disk, the URL is valid for as long as that location exists. You can delete the file and recreate it, and it's all good.

You can supply the mimeType to simulate the optional MIME-type header associated with HTTP downloads.

DOMString toURL (
  in optional DOMString mimeType
);
Parameter
mimeType
The MIME type that you want to be used to interpret the file when it is loaded through this URL.
Returns
DOMString

remove()

Deletes a file or directory. You cannot delete an empty directory or the root directory of a file system. If you want to remove an empty directory, use removeRecursively() instead.

void remove (
  in VoidCallback successCallback, optional ErrorCallback errorCallback
);
Parameter
successCallback
A callback that is called when the file is successfully deleted.
errorCallback
An optional callback that is called only when errors happen.
Returns
void

getParent()

Look up the parent DirectoryEntry containing the entry. If this entry is the root of its file system, then the parent is itself.

void getParent (
 in EntryCallback successCallback, optional ErrorCallback errorCallback
);
Parameter
parent
The directory where you want to move the entry.
newName
The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.
successCallback
A callback that is called when the look-up entry is successful.
errorCallback
An optional 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)}}[1] {{CompatNo}} {{CompatNo}} {{CompatNo}}
copyTo(), getMetadata(), getParent(), moveTo(), remove(), toURL() 13 {{property_prefix("webkit")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
filesystem: URL scheme 13 {{property_prefix("webkit")}} {{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)}}[1] {{CompatNo}} {{CompatNo}} {{CompatNo}}
copyTo(), getMetadata(), getParent(), moveTo(), remove(), toURL() {{CompatNo}} 0.16 {{property_prefix("webkit")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
filesystem: URL scheme {{CompatNo}} 0.16 {{property_prefix("webkit")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] In Firefox, the error callback's parameter is a {{domxref("DOMError")}} rather than a {{domxref("FileError")}} object.

See also

Revision Source

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

<p>The <strong><code>FileSystemEntry</code></strong> interface of the File and Directory Entries API represents a single in a file system. The entry can be a file or a directory (directories are represented by the {{domxref("DirectoryEntry")}} interface. It includes methods for working with files—including copying, moving, removing, and reading files—as well as information about a file it points to—including the file name and its path from the root to the entry.</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" name="basic">Basic concepts</h2>

<p>You don't create <code>FileSystemEntry</code> objects directly. Instead, you will receive an object based on this interface through other APIs. This interface serves as a base class for the {{domxref("FileSystemFileEntry")}} and {{domxref("FileSystemDirectoryEntry")}} interfaces, which provide features specific to file system entries representing files and directories, respectively.</p>

<p>The <code>FileSystemEntry</code> interface includes methods that you would expect for manipulating files and directories, but it also includes a convenient method for obtaining the URL of the entry: <code><a href="#toURL">toURL()</a></code>. It also introduces a new URL scheme: <code>filesystem:</code>.</p>

<p>You can use the <code>filesystem:</code> scheme on Google Chrome to see all the files and folders that are stored in the origin of your app. Just use <code>filesystem:</code> scheme for the root directory of the origin of the app. For example, if your app is in <code><a href="https://www.html5rocks.com" rel="freelink">https://www.html5rocks.com</a></code>, open<code> filesystem:<a href="https://www.html5rocks.com/temporary/" rel="freelink">https://www.html5rocks.com/temporary/</a></code> in a tab. Chrome shows a read-only list of all the files and folders stored the origin of your app.</p>

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

<p>To see an example of how <code>toURL()</code> works, see the <a href="#toURL">method description</a>. The snippet below shows you how you can remove a file by name.</p>

<pre class="brush: js">
// Taking care of the browser-specific prefixes.
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem; 

...

// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, function(fs) {
  fs.root.getFile('log.txt', {}, function(fileEntry) {
    
    fileEntry.remove(function() {
      console.log('File removed.');
    }, onError);
  
  }, onError);
}, onError); </pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>void <a href="#getMetadata">getMetadata</a> (in MetadataCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#moveTo">moveTo</a> (in DirectoryEntry <em>parent</em>, optional DOMString <em>newName</em>, in optional EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#copyTo">copyTo</a> (in DirectoryEntry <em>parent</em>, in optional DOMString <em>newName</em>, in optional EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#toURL">toURL</a> (in optional DOMString <em>mimeType</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#remove">remove</a> (in VoidCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#getParent">getParent</a> (in EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
 </tbody>
</table>

<h2 id="Properties">Properties</h2>

<p><em>This interface provides the following properties.</em></p>

<dl>
 <dt>filesystem {{ReadOnlyInline}}</dt>
 <dd>A {{domxref("FileSystem")}} object representing the file system in which the entry is located.</dd>
 <dt>fullPath {{ReadOnlyInline}}</dt>
 <dd>A {{domxref("DOMString")}} object which provides the full, absolute path from the file system's root to the entry; it can also be thought of as a path which is relative to the root directory, prepended with a "/" character.</dd>
 <dt>isDirectory {{ReadOnlyInline}}</dt>
 <dd>A {{jsxref("Boolean")}} which is <code>true</code> if the entry represents a directory; otherwise, it's <code>false</code>.</dd>
 <dt>isFile {{ReadOnlyInline}}</dt>
 <dd>A Boolean which is <code>true</code> if the entry represents a file. If it's not a file, this value is <code>false</code>.</dd>
 <dt>name {{ReadOnlyInline}}</dt>
 <dd>A {{domxref("DOMString")}} containing the name of the entry (the final part of the path, after the last "/" character).</dd>
</dl>

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

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

<p>Look up last modification date of the entry.</p>

<pre>
void getMetadata (
  in MetadataCallback ErrorCallback
);</pre>

<h5 id="Parameter">Parameter</h5>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called with the time of the last modification.</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="moveTo" name="moveTo">moveTo()</h3>

<p>Move an entry to a different location on the file system. Moving a file over an existing file replaces that existing file. A move of a directory on top of an existing empty directory replaces that directory.</p>

<p>You can also use this method for renaming files. You can keep it in the same location and then define the <code>newName</code> parameter.</p>

<p>You cannot do the following:</p>

<ul>
 <li>Move a directory inside itself or to any child at any depth</li>
 <li>Move an entry into its parent if a name different from its current one isn't provided</li>
 <li>Move a file to a path occupied by a directory or move a directory to a path occupied by a file</li>
 <li>Move any element to a path occupied by a directory that is not empty.</li>
</ul>

<pre>
void moveTo (
  in DirectoryEntry <em>parent</em>, optional DOMString <em>newName</em>, optional EntryCallback <em>successCallback</em>, optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter_2">Parameter</h5>

<dl>
 <dt>parent</dt>
 <dd>The directory to which to move the entry.</dd>
 <dt>newName</dt>
 <dd>The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.</dd>
 <dt>successCallback</dt>
 <dd>A callback that is called when the file or directory has been successfully moved.</dd>
 <dt>errorCallback</dt>
 <dd>An optional callback that is called only when errors happen.</dd>
</dl>

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

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

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

<p>Copy an entry to a different location on the file system. You cannot copy an entry inside itself if it is a directory, nor can you copy it into its parent if you don't provide a new name. Directory copies are always recursive—that is, they copy all contents of the directory. You cannot change this behavior. Files are simply duplicated.</p>

<pre>
void copyTo (
  in DirectoryEntry <em>parent</em>, optional DOMString <em>newName</em>, optional EntryCallback <em>successCallback</em>, optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter_3">Parameter</h5>

<dl>
 <dt>parent</dt>
 <dd>The directory where you want the entry to move to.</dd>
 <dt>newName</dt>
 <dd>The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.</dd>
 <dt>successCallback</dt>
 <dd>A callback that is called when the file or directory has been successfully copied.</dd>
 <dt>errorCallback</dt>
 <dd>An optional callback that is called only when errors happen.</dd>
</dl>

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

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

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

<p>Returns a URL that can be used to identify this entry. It exposes a new URL scheme—<code>filesystem:</code>—that you can use to fill <code>src</code> or <code>href</code> attributes. For example, if you wanted to display an image and have its <a href="/en-US/docs/DOM/File_API/File_System_API/FileEntry">fileEntry</a>, calling <code>toURL()</code> gives you the image file's file system URL. You get something like: <code>filesystem:<a href="https://example.com/temporary/lolcat.png" rel="freelink">https://example.com/temporary/lolcat.png</a>.</code></p>

<p>The following is an example:</p>

<pre class="brush: js">
var img = document.createElement('img'); 
// Get the URL of the entry, which returns: filesystem:<a href="https://example.com/temporary/lolcat.png" rel="freelink">https://example.com/temporary/lolcat.png</a> 
img.src = fileEntry.toURL(); 
document.body.appendChild(img);</pre>

<p>The file system URL does not expire. Because the method describes a location on disk, the URL is valid for as long as that location exists. You can delete the file and recreate it, and it's all good.</p>

<p>You can supply the <code>mimeType</code> to simulate the optional MIME-type header associated with HTTP downloads.</p>

<pre>
DOMString toURL (
  <em>in </em>optional DOMString mimeType
);</pre>

<h5 id="Parameter_4">Parameter</h5>

<dl>
 <dt>mimeType</dt>
 <dd>The MIME type that you want to be used to interpret the file when it is loaded through this URL.</dd>
</dl>

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

<dl>
 <dt><code>DOMString</code></dt>
</dl>

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

<p>Deletes a file or directory. You cannot delete an empty directory or the root directory of a file system. If you want to remove an empty directory, use <a href="/en-US/docs/DOM/File_API/File_System_API/DirectoryEntrySync#removeRecursively()"><code>removeRecursively()</code></a> instead.</p>

<pre>
void remove (
  in VoidCallback <em>successCallback</em>, optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter_5">Parameter</h5>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called when the file is successfully deleted.</dd>
 <dt>errorCallback</dt>
 <dd>An optional callback that is called only when errors happen.</dd>
</dl>

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

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

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

<p>Look up the parent <code>DirectoryEntry</code> containing the entry. If this entry is the root of its file system, then the parent is itself.</p>

<pre>
void getParent (
 in EntryCallback <em>successCallback</em>, optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter_6">Parameter</h5>

<dl>
 <dt>parent</dt>
 <dd>The directory where you want to move the entry.</dd>
 <dt>newName</dt>
 <dd>The new name of the entry. If you do not specify a name, the browser preserves the entry's current name.</dd>
 <dt>successCallback</dt>
 <dd>A callback that is called when the look-up entry is successful.</dd>
 <dt>errorCallback</dt>
 <dd>An optional callback that is called when errors happen.</dd>
</dl>

<h5 id="Returns_6">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>

<div>{{CompatibilityTable}}</div>

<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)}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>copyTo()</code>, <code>getMetadata()</code>, <code>getParent()</code>, <code>moveTo()</code>, <code>remove()</code>, <code>toURL()</code></td>
   <td>13 {{property_prefix("webkit")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>filesystem: URL scheme</td>
   <td>13 {{property_prefix("webkit")}}</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 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)}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>copyTo()</code>, <code>getMetadata()</code>, <code>getParent()</code>, <code>moveTo()</code>, <code>remove()</code>, <code>toURL()</code></td>
   <td>{{CompatNo}}</td>
   <td>0.16 {{property_prefix("webkit")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>filesystem:</code> URL scheme</td>
   <td>{{CompatNo}}</td>
   <td>0.16 {{property_prefix("webkit")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] In Firefox, the error callback's parameter is a {{domxref("DOMError")}} rather than a {{domxref("FileError")}} object.</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