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")}}.