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")}} 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(); // 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 }} |
createWriter() |
13 {{ property_prefix("webkit") }} | {{ CompatNo }}[1] | {{ 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.