1. Goals
This specification documents the types and operations made available by web browsers to script when a hierarchy of files and directories are dragged and dropped onto a page or selected using form elements, or equivalent user actions.
This is heavily based upon earlier drafts of [file-system-api] which defines similar types in the context of a sandboxed file system, including operations for creating and modifying files and directories, but which has not been broadly adopted by web browsers.
note: The APIs described by this document was initially implemented in Google Chrome. Other browsers (at this time: Edge, Firefox and Safari) are starting to support subsets of Chrome’s APIs and behavior. The intent of this document is to specify the common subset to ensure that the implementations are interoperable.
2. Concepts
2.1. Names and Paths
A name is a string which:
-
does not contain '/' (U+002F SOLIDUS)
-
does not contain NUL (U+0000)
-
does not contain '\' (U+005C REVERSE SOLIDUS)
-
is not '.' (U+002E FULL STOP)
-
is not '..' (U+002E FULL STOP, U+002E FULL STOP)
A path segment is a name, '.' (U+002E FULL STOP) or '..' (U+002E FULL STOP, U+002E FULL STOP).
A relative path is a string consisting of one or more path segments joined by '/' (U+002F SOLIDUS) that does not start with '/' (U+002F SOLIDUS).
An absolute path is a string consisting of '/' (U+002F SOLIDUS) followed by zero or more path segments joined by '/' (U+002F SOLIDUS).
A path is either a relative path or an absolute path.
A valid path is a USVString
which is a path.
2.2. Files and Directories
A file consists of binary data and a name (a non-empty name).
A directory consists of a name (a name) and an ordered list of members. Each member is either a file or a directory. Each member of a directory must have a distinct non-empty name.
A root directory is a directory that is not a member of a directory. A root directory's name is empty.
The parent of a file or directory is the directory it is a member of. A root directory has no parent.
note: In most cases, the files and directories selected by the user will be presented by the API as if contained by a virtual root that does not exist as an entity in the actual native file system backing the interaction with the API.
A file system consists of a name and a root which is an associated root
directory. The name of a file system is a USVString
which is implementation defined but is unique to the file system. A root directory is associated with exactly one file system.
note: Implementations could produce a name by generating a UUID for each file system instance with some fixed prefix and suffix strings applied. Authors using the API are adviised not to make assumptions about the structure or content of the names.
2.3. Entries
An entry is either a file entry or a directory entry.
An entry has an name (a name) and a full path (an absolute path).
An entry also has a root, which is an associated root directory.
note: Entries are defined in terms of paths relative to a root directory to account for the fact that a native file system backing the interaction with the API could be modified asynchronously during operations such as enumerating the contents of a directory. Operations exposed on entries will produce errors in such cases where the paths no longer reference the same entity.
The file system of an entry is the file system associated with the entry's root.
3. Algorithms
To resolve a relative path with abspath (an absolute path) and path (an absolute path, a relative path, or the empty string), perform the following steps. They return an absolute path.
-
If path is an absolute path, return path.
-
Let abspath segments be the result of strictly splitting abspath on '/' (U+002F SOLIDUS).
note: The first string will be empty.
-
Let path segments be the result of strictly splitting path on '/' (U+002F SOLIDUS).
-
For each segment in path segments, switch on segment:
- empty string
-
Continue.
- '.' (U+002E FULL STOP)
-
Continue.
- '..' (U+002E FULL STOP, U+002E FULL STOP)
-
Remove the last member of abspath segments unless it is the only member.
- Otherwise
-
Append segment to abspath segments.
-
Return abspath segments joined by '/' (U+002F SOLIDUS).
To evaluate a path with directory (an root directory) and path (an absolute path), perform the following steps. They return a file, directory, or failure.
-
Let segments be the result of strictly splitting path on '/' (U+002F SOLIDUS).
-
Remove the first entry from segments.
note: Since path was an absolute path, this first entry will always be empty.
-
For each segment in segments, switch on segment:
- empty string
-
Continue.
- '.' (U+002E FULL STOP)
-
Continue.
- '..' (U+002E FULL STOP, U+002E FULL STOP)
-
Let directory be directory’s parent, or directory if none.
- Otherwise
-
Run these steps:
4. The File
Interface
partial interface File {readonly attribute USVString webkitRelativePath ; };
The webkitRelativePath
getter steps are to return this's relative path, or the empty string if not specified.
5. HTML: Forms
webkitRelativePath
property. partial interface HTMLInputElement {attribute boolean ;
webkitdirectory readonly attribute FrozenArray <FileSystemEntry >; };
webkitEntries
When an input
element’s type
attribute is in the File
Upload state, the rules in this section apply.
The webkitdirectory
attribute is a boolean
attribute that indicates whether the user is to be allowed to select a
directory rather than a file or files. When specified, the behavior on
the selection of a directory is as if all files with that directory as
an ancestor were selected. In addition, the webkitRelativePath
property of each File
is set to a relative path starting from (and including) the selected directory
to the file.
documents/ to_upload/ a/ b/ 1.txt 2.txt 3.txt not_uploaded.txt
If the to_upload
directory was selected, then files
would include:
-
An entry with
name
== "1.txt
" andwebkitRelativePath
== "to_upload/a/b/1.txt
" -
An entry with
name
== "2.txt
" andwebkitRelativePath
== "to_upload/a/b/2.txt
" -
An entry with
name
== "3.txt
" andwebkitRelativePath
== "to_upload/a/3.txt
"
note: A user agent could represent any hierarchical data as directories
during a selection operation. For example, on a device that does not
expose a native file system directly to the user, photo albums could
be presented as directories if "image/*"
is specified
for the accept
attribute.
webkitRelativePath
properties after a
directory is selected with an input
element:
< input id = b type = file webkitdirectory >
document. querySelector( '#b' ). addEventListener( 'change' , e=> { for ( file entryof e. target. files) { console. log( file. name, file. webkitRelativePath); } });
The webkitEntries
IDL attribute allows scripts to
access the element’s selected entries. On getting, if the IDL
attribute applies, it must return an array of FileSystemEntry
objects that represent the current selected files (including
directories, if permitted). If the IDL attribute does not apply, then
it must instead return null.
webkitEntries
:
< input id = a type = file multiple >
document. querySelector( '#a' ). addEventListener( 'change' , e=> { for ( const entryof e. target. webkitEntries) { handleEntry( entry); } });
webkitEntries
is only populated as
the result of a drag-and-drop operation, not when the element is
clicked. Should we fix this so it is always populated? webkitdirectory
is specified on a HTMLInputElement
, webkitEntries
is not
populated; the files
collection and webkitRelativePath
properties must be used instead to
reconstruct the directory structure. Should we fix this so it is
always populated? 6. HTML: Drag and drop
During a drag-and-drop operation, file and directory items are associated with entries. Each entry is a member of a root directory unique to the drag data store.
Additionally, each directory item is represented in the drag
data store item list as an item whose kind is File. If it is accessed via getAsFile()
a zero-length File
is returned.
note: A user agent could represent any hierarchical data as files and directories during a drag-and-drop operation. For example, audio data stored in a relational database with separate tables for albums metadata and blobs for tracks could be exposed to script as directories and files when dragged from a media player application.
partial interface DataTransferItem {FileSystemEntry ?webkitGetAsEntry (); };
The webkitGetAsEntry()
method steps are:
-
Let store be this's
DataTransfer
object’s drag data store. -
If store’s drag data store mode is not read/write mode or read-only mode, return null and abort these steps.
-
Let item be the item in store’s drag data store item list that this represents.
-
If item’s kind is not File, then return null and abort these steps.
-
Return a new
FileSystemEntry
object representing the entry.
elem. addEventListener( 'dragover' , e=> { // Prevent navigation. e. preventDefault(); }); elem. addEventListener( 'drop' , e=> { // Prevent navigation. e. preventDefault(); // Process all of the items. for ( const itemof e. dataTransfer. items) { // kind will be 'file' for file/directory entries. if ( item. kind=== 'file' ) { const entry= item. webkitGetAsEntry(); handleEntry( entry); } } });
7. Files and Directories
TypeMismatchError
has been replaced in most
specifications by TypeError
, but the name differs. Is it
compatible to switch here as well? callback =
ErrorCallback undefined (DOMException );
err
An ErrorCallback
function is used for operations that may return an
error asynchronously.
-
The file reading task source [FileAPI], for
readEntries()
success and error callbacks. (The Chromium implementation calls thisTaskType::kFileReading
.) -
The DOM manipulation task source [HTML] elsewhere. (The Chromium implementation uses
TaskType::kMiscPlatformAPI
which targets the same underlying task queue asTaskType::kDOMManipulation
.)
7.1. The FileSystemEntry
Interface
[Exposed =Window ]interface {
FileSystemEntry readonly attribute boolean isFile ;readonly attribute boolean isDirectory ;readonly attribute USVString name ;readonly attribute USVString fullPath ;readonly attribute FileSystem filesystem ;undefined getParent (optional FileSystemEntryCallback ,
successCallback optional ErrorCallback ); };
errorCallback
An FileSystemEntry
has an associated entry.
The isFile
getter steps are to return true if this is a file entry and false otherwise.
The isDirectory
getter steps are to return true if this is a directory entry and false otherwise.
The name
getter steps are to return this's name.
The fullPath
getter steps are to return this's full path.
The filesystem
getter steps are to return this's file system.
The getParent(successCallback, errorCallback)
method steps are:
-
In parallel, run these steps:
-
Let path be the result of resolving a relative path with this's full path and '..'.
-
Let item be the result of evaluating a path with this's root and path.
-
If item is failure, queue a task to invoke errorCallback (if given) with a newly created "
NotFoundError
"DOMException
, and abort these steps. -
Let entry be a new directory entry with item’s name as name and path as full path.
-
Queue a task to invoke successCallback with a new
FileSystemDirectoryEntry
object associated with entry.
-
note: An error is possible if files have been modified on disk since the FileSystemEntry
was created.
function handleEntry( entry) { console. log( 'name: ' + entry. name); console. log( 'path: ' + entry. fullPath); if ( entry. isFile) { console. log( '... is a file' ); } else if ( entry. isDirectory) { console. log( '... is a directory' ); } }
getParent()
for use with Promises [ECMA-262]:
function getParentAsPromise( entry) { return new Promise(( resolve, reject) => { entry. getParent( resolve, reject); }); }
7.2. The FileSystemDirectoryEntry
Interface
[Exposed =Window ]interface :
FileSystemDirectoryEntry FileSystemEntry {FileSystemDirectoryReader createReader ();undefined getFile (optional USVString ?,
path optional FileSystemFlags = {},
options optional FileSystemEntryCallback ,
successCallback optional ErrorCallback );
errorCallback undefined getDirectory (optional USVString ?,
path optional FileSystemFlags = {},
options optional FileSystemEntryCallback ,
successCallback optional ErrorCallback ); };
errorCallback dictionary {
FileSystemFlags boolean =
create false ;boolean =
exclusive false ; };callback =
FileSystemEntryCallback undefined (FileSystemEntry );
entry
note: The create
member of FileSystemFlags
and
the associated behavior are included for compatibility with existing
implementations, even though there is no useful behavior when the
flag is specified. Similarly, the exclusive
member is not explicitly referenced, but the binding behavior is
observable from script if an object with a getter is passed.
A FileSystemDirectoryEntry
's associated entry is a directory
entry.
The createReader()
method steps are:
-
Return a newly created
FileSystemDirectoryReader
object associated with directory entry.
The getFile(path, options, successCallback, errorCallback)
method steps are:
-
In parallel, run these steps:
-
If path is undefined or null let path be the empty string.
-
If path is not a valid path, queue a task to invoke errorCallback (if given) with a newly created "
TypeMismatchError
"DOMException
, and abort these steps. -
If options’s
create
member is true, queue a task to invoke errorCallback (if given) with a newly created "SecurityError
"DOMException
, and abort these steps. -
Let path be the result of resolving a relative path with this's full path and path.
-
Let item be the result of evaluating a path with this's root and path.
-
If item is failure, queue a task to invoke errorCallback (if given) with a newly created "
NotFoundError
"DOMException
, and abort these steps. -
If item is not a file, queue a task to invoke errorCallback (if given) with a newly created "
TypeMismatchError
"DOMException
, and abort these steps. -
Let entry be a new file entry with item’s name as name and path as full path.
-
Queue a task to invoke successCallback (if given) with a new
FileSystemFileEntry
object associated with entry.
-
The getDirectory(path, options, successCallback, errorCallback)
method steps are:
-
In parallel, run these steps:
-
If path is undefined or null let path be the empty string.
-
If path is not a valid path, queue a task to invoke errorCallback (if given) with a newly created "
TypeMismatchError
"DOMException
, and abort these steps. -
If options’s
create
member is true, queue a task to invoke errorCallback (if given) with a newly created "SecurityError
"DOMException
, and abort these steps. -
Let path be the result of resolving a relative path with this's full path and path.
-
Let item be the result of evaluating a path with this's root and path.
-
If item is failure, queue a task to invoke errorCallback (if given) with a newly created "
NotFoundError
"DOMException
, and abort these steps. -
If item is not a directory, invoke errorCallback (if given) with a newly created "
TypeMismatchError
"DOMException
, and abort these steps. -
Let entry be a new directory entry with item’s name as name and path as full path.
-
Queue a task to invoke successCallback (if given) with a new
FileSystemDirectoryEntry
associated with entry.
-
getFile()
and getDirectory()
for use
with Promises [ECMA-262]:
function getFileAsPromise( entry, path) { return new Promise(( resolve, reject) => { entry. getFile( path, {}, resolve, reject); }); } function getDirectoryAsPromise( entry, path) { return new Promise(( resolve, reject) => { entry. getDirectory( path, {}, resolve, reject); }); }
7.3. The FileSystemDirectoryReader
Interface
[Exposed =Window ]interface {
FileSystemDirectoryReader undefined (
readEntries FileSystemEntriesCallback ,
successCallback optional ErrorCallback ); };
errorCallback callback =
FileSystemEntriesCallback undefined (sequence <FileSystemEntry >);
entries
FileSystemDirectoryReader
has
an associated entry (a directory entry),
an associated directory (initially null),
a reading flag (initially false),
a done flag (initially false),
and a reader error (initially null). The readEntries(successCallback, errorCallback)
method steps are:
-
If this's reading flag is true, queue a task to invoke errorCallback with a newly created "
InvalidStateError
"DOMException
, and abort these steps. -
If this's reader error is not null, queue a task to invoke errorCallback (if given) with reader error, and abort these steps.
-
If this's done flag is true, queue a task to invoke successCallback with an empty sequence and abort these steps.
-
Set this's reading flag to true.
-
In parallel, run these steps:
-
If this's directory is null, then:
-
Let dir be the result of evaluating a path with this's entry's root and full path.
-
If dir is failure, then:
-
Queue a task to run these steps:
-
Let error be a newly created "
NotFoundError
"DOMException
. -
Set this's reader error to error.
-
Set this's reading flag to false.
-
Invoke errorCallback (if given) with error.
-
-
Abort these steps.
-
-
-
Let entries be a non-zero number of entries from this's directory that have not yet been produced by this
FileSystemDirectoryReader
, if any. -
If the previous step failed (for example, the directory was deleted or permission is denied), then:
-
Queue a task to run these steps:
-
Let error be a an appropriate
DOMException
. -
Set this's reader error to error.
-
Set this's reading flag to false.
-
Invoke errorCallback (if given) with reader error.
-
-
Abort these steps.
-
-
Queue a task to run these steps:
-
Set this's reading flag to false.
-
Invoke successCallback with entries.
-
NOTE: The use of the the reading flag prevents multiple copies of the in parallel steps above from executing simultaneously. This obviates the need to specify a parallel queue.
const reader= dirEntry. createReader(); const doBatch= () => { // Read a batch. reader. readEntries( entries=> { // Complete? if ( entries. length=== 0 ) { return ; } // Process the batch. entries. forEach( handleEntry); // Read the next batch. doBatch(); }, error=> console. warn( error)); }; // Start reading doBatch();
FileSystemDirectoryReader
for use with Promises [ECMA-262]:
function getEntriesAsPromise( dirEntry) { return new Promise(( resolve, reject) => { const result= []; const reader= dirEntry. createReader(); const doBatch= () => { reader. readEntries( entries=> { if ( entries. length> 0 ) { entries. forEach( e=> result. push( e)); doBatch(); } else { resolve( result); } }, reject); }; doBatch(); }); }
FileSystemDirectoryReader
for use with AsyncIterators [ECMA-262]:
async function * getEntriesAsAsyncIterator( dirEntry) { const reader= dirEntry. createReader(); const getNextBatch= () => new Promise(( resolve, reject) => { reader. readEntries( resolve, reject); }); let entries; do { entries= await getNextBatch(); for ( const entryof entries) { yield entry; } } while ( entries. length> 0 ); }
This allows for ordered asynchronous traversal of a directory tree
using for-await-of
:
async function show( entry) { console. log( entry. fullPath); if ( entry. isDirectory) { for await ( const eof getEntriesAsAsyncIterator( entry)) { await show( e); } } }
7.4. The FileSystemFileEntry
Interface
[Exposed =Window ]interface :
FileSystemFileEntry FileSystemEntry {undefined file (FileCallback ,
successCallback optional ErrorCallback ); };
errorCallback callback =
FileCallback undefined (File );
file
A FileSystemFileEntry
's associated entry is a file entry.
The file(successCallback, errorCallback)
method steps are:
-
In parallel, run these steps:
-
Let item be the result of evaluating a path with this's root and full path.
-
If item is failure, queue a task to invoke errorCallback (if given) with a newly created "
NotFoundError
"DOMException
, and abort these steps. -
If item is a directory, queue a task to invoke errorCallback (if given) with a newly created "
TypeMismatchError
"DOMException
, and abort these steps. -
Queue a task to invoke successCallback with a new
File
object representing item.
-
FileReader
:
function readFileEntry( entry) { entry. file( file=> { const reader= new FileReader(); reader. readAsText( file); reader. onerror= error=> console. warn( error); reader. onload= () => { console. log( reader. result); }; }, error=> console. warn( error)); }
file()
for use
with Promises [ECMA-262]:
function fileAsPromise( entry) { return new Promise(( resolve, reject) => { entry. file( resolve, reject); }); }
7.5. The FileSystem
Interface
[Exposed =Window ]interface {
FileSystem readonly attribute USVString name ;readonly attribute FileSystemDirectoryEntry root ; };
A FileSystem
has an associated file system.
The name
getter steps are to return this's name.
The root
getter steps are to return a FileSystemDirectoryEntry
associated with this's root.
8. Acknowledgements
This specification is based heavily on the work of Eric Uhrhane in [file-system-api], which introduced the FileSystemEntry
types.
Thanks to Tab Atkins, Jr. for creating and maintaining Bikeshed, the specification authoring tool used to create this document.
And thanks to Ali Alabbas, Philip Jägenstedt, Marijn Kruisselbrink, Olli Pettay, and Kent Tamura for suggestions, reviews, and other feedback.