这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
FileSystemDirectoryEntry
接口方法
getFile
()
返回
FileSystemFileEntry
object corresponding to a file contained somewhere within the directory subtree rooted at the directory on which it's called.
FileSystemDirectoryEntry.getFile([path][, options][, successCallback][, errorCallback]);
path
可选
USVString
specifying the path, relative to the directory on which the method is called, describing which file's entry to return.
选项
可选
FileSystemFlags
dictionary, which allows you to specify whether or not to create the entry if it's missing and if it's an error if the file already exists. These options are currently not useful in Web contexts.
successCallback
可选
FileSystemFileEntry
has been created. The method receives a single parameter: the
FileSystemFileEntry
object representing the file in question.
errorCallback
可选
FileError
object describing the error which occurred.
If an error occurs and an
errorCallback
was specified, it gets called with a single parameter: a
FileError
object describing the error. The {domxref("FileError.code")}} specifies what type of error occurred, as follows:
FileError.NOT_FOUND_ERR
create
option was not specified (or was specified as
false
), and the file doesn't exist.
FileError.PATH_EXISTS_ERR
create
and
exclusive
options were both
true
, indicating that the file should be created but must not already exist, but the file does in fact already exist.
FileError.SECURITY_ERR
The request to access the file was denied for security reasons.
FileError.TYPE_MISMATCH_ERR
The path specified is not a file; it's probably a directory, but might be an unsupported file descriptor such as a pipe; this depends on the user agent to some extent.
选项
parameter is an object which is based on the
FileSystemFlags
dictionary; it provides flags which make it possible to adjust the behavior of the
getFile()
方法。
create
可选
true
, and the requested file or directory doesn't exist, the user agent should create it. The default is
false
. The parent directory must already exist.
exclusive
可选
true
,和
create
option is also
true
, the file must not exist prior to issuing the call. Instead, it must be possible for it to be created newly at call time. The default is
false
.
The table below describes the result of each possible combination of these flags depending on whether or not the target file or directory path already exists.
| Option values | File/directory condition | 结果 | ||
|---|---|---|---|---|
create
|
exclusive
|
|||
false
|
n/a [1] |
Path exists and matches the desired type (depending on whether the function called is
getFile()
or
getDirectory()
|
successCallback
is called with a
FileSystemFileEntry
if
getFile()
was called or a
FileSystemDirectoryEntry
if
getDirectory()
被调用。
|
|
false
|
n/a [1] | Path exists but doesn't match the desired type |
errorCallback
is called with an appropriate error code (if the callback was provided).
|
|
true
|
false
|
Path exists |
The existing file or directory is removed and replaced with a new one, then the
successCallback
is called with a
FileSystemFileEntry
或
FileSystemDirectoryEntry
, as appropriate.
|
|
true
|
false
|
Path doesn't exist |
The file or directory is created, then a
FileSystemFileEntry
或
FileSystemDirectoryEntry
被传递给
successCallback
, as appropriate.
|
|
true
|
true
|
Path exists |
errorCallback
is called with an appropriate error, such as
FileError.PATH_EXISTS_ERR
.
|
|
true
|
true
|
Path doesn't exist |
The file or directory is created, then a
FileSystemFileEntry
或
FileSystemDirectoryEntry
被传递给
successCallback
, as appropriate.
|
|
[1] When
create
is
false
,值对于
exclusive
is irrelevant and ignored.
In this example, a function is presented whose job it is to locate within a user's app data directory a JSON file containing a user dictionary for a specified language, then load that dictionary.
let dictionary = null;
function loadDictionaryForLanguage(appDataDirEntry, lang) {
dictionary = null;
appDataDirEntry.getDirectory("Dictionaries", {}, function(dirEntry) {
dirEntry.getFile(lang + "-dict.json", {}, function(fileEntry) {
fileEntry.file(function(dictFile)) {
let reader = new FileReader();
reader.addEventListener("loadend", function() {
dictionary = JSON.parse(reader.result);
});
reader.readAsText(dictFile);
});
});
});
}
loadDictionaryForLanguage()
function starts by using
getDirectory()
to obtain the
FileSystemDirectoryEntry
object representing a subfolder named "Dictionaries" located inside the specified app data directory. The success callback for this takes the resulting directory entry object and calls
getFile()
to get a
FileSystemFileEntry
object representing the dictionary file; the success callback for this, in turn, creates a new
FileReader
and uses it to load the contents of the file. When that is loaded successfully (as indicated by the
loadend
event being fired), the loaded text is passed into
JSON.parse()
to be reconstituted into a JavaScript object.
| 规范 | 状态 | 注释 |
|---|---|---|
|
文件和目录条目 API
The definition of 'getFile()' in that specification. |
草案 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getFile
|
Chrome 8 | Edge 79 |
Firefox
50
|
IE No | Opera No | Safari 11.1 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
50
|
Opera Android No | Safari iOS 11.3 | Samsung Internet Android Yes |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
FileSystemDirectoryEntry
createReader()
getDirectory()
getFile()
removeRecursively()
FileError
FileException
FileHandle
FileRequest
FileSystem
FileSystemDirectoryEntrySync
FileSystemDirectoryReader
FileSystemDirectoryReaderSync
FileSystemEntry
FileSystemEntrySync
FileSystemFileEntry
FileSystemFileEntrySync
FileSystemFlags
FileSystemSync
HTMLInputElement
LocalFileSystem
LocalFileSystemSync
LockedFile
元数据
Window.requestFileSystem()
Window.resolveLocalFileSystemURL()
WorkerGlobalScope.requestFileSystemSync()