1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
---
license: Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
---
FileEntry
==========
Represents a file on a file system, as defined in the
[W3C Directories and Systems](http://www.w3.org/TR/file-system-api/)
specification.
Properties
----------
- __isFile__: Always true. _(boolean)_
- __isDirectory__: Always false. _(boolean)_
- __name__: The name of the `FileEntry`, excluding the path leading to it. _(DOMString)_
- __fullPath__: The full absolute path from the root to the `FileEntry`. _(DOMString)_
__NOTE:__ The following attribute is defined by the W3C specification,
but is _not_ supported:
- __filesystem__: The file system on which the `FileEntry` resides. _(FileSystem)_
Methods
-------
- __getMetadata__: Look up metadata about a file.
- __setMetadata__: Set metadata on a file.
- __moveTo__: Move a file to a different location on the file system.
- __copyTo__: Copy a file to a different location on the file system.
- __toURL__: Return a URL that can be used to locate a file.
- __remove__: Delete a file.
- __getParent__: Look up the parent directory.
- __createWriter__: Creates a `FileWriter` object that can be used to write to a file.
- __file__: Creates a `File` object containing file properties.
Supported Platforms
-------------------
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
- iOS
- Windows Phone 7 and 8
- Windows 8
getMetadata
----------------
Look up metadata about a file.
__Parameters:__
- __successCallback__: A callback that is passed a `Metadata` object. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when retrieving the `Metadata`. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(metadata) {
console.log("Last Modified: " + metadata.modificationTime);
}
function fail(error) {
alert(error.code);
}
// Request the metadata object for this entry
entry.getMetadata(success, fail);
setMetadata
----------------
Set metadata on a file.
__Currently works only on iOS.__
- this will set the extended attributes of a file.
__Parameters:__
- __successCallback__: A callback that executes when the metadata is set. _(Function)_
- __errorCallback__: A callback that executes when the metadata is not successfully set. _(Function)_
- __metadataObject__: An object that contains the metadata's keys and values. _(Object)_
__Quick Example__
function success() {
console.log("The metadata was successfully set.");
}
function fail() {
alert("There was an error in setting the metadata");
}
// Set the metadata
entry.setMetadata(success, fail, { "com.apple.MobileBackup": 1});
__iOS Quirk__
- Only the `com.apple.MobileBackup` extended attribute is supported. Set the value to `1` to prevent the file from being backed up to iCloud. Set the value to `0` to re-enable the file to be backed up to iCloud.
__Quick Example__
function setFileMetadata(localFileSystem, filePath, metadataKey, metadataValue)
{
var onSetMetadataWin = function() {
console.log("success setting metadata")
}
var onSetMetadataFail = function() {
console.log("error setting metadata")
}
var onGetFileWin = function(parent) {
var data = {};
data[metadataKey] = metadataValue;
parent.setMetadata(onSetMetadataWin, onSetMetadataFail, data);
}
var onGetFileFail = function() {
console.log("error getting file")
}
var onFSWin = function(fileSystem) {
fileSystem.root.getFile(filePath, {create: true, exclusive: false}, onGetFileWin, onGetFileFail);
}
var onFSFail = function(evt) {
console.log(evt.target.error.code);
}
window.requestFileSystem(localFileSystem, 0, onFSWin, onFSFail);
}
setFileMetadata(LocalFileSystem.PERSISTENT, "Backups/sqlite.db", "com.apple.MobileBackup", 1);
moveTo
------
Move a file to a different location on the file system. An error
results if the app attempts to:
- move a file into its parent if a name different from its current one isn't provided;
- move a file to a path occupied by a directory;
In addition, moving a file on top of an existing file attempts to
delete and replace that file.
__Parameters:__
- __parent__: The parent directory to which to move the file. _(DirectoryEntry)_
- __newName__: The new name of the file. Defaults to the current name if unspecified. _(DOMString)_
- __successCallback__: A callback that is passed the new files `FileEntry` object. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when attempting to move the file. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(entry) {
console.log("New Path: " + entry.fullPath);
}
function fail(error) {
alert(error.code);
}
function moveFile(entry) {
var parent = document.getElementById('parent').value,
parentName = parent.substring(parent.lastIndexOf('/')+1),
parentEntry = new DirectoryEntry(parentName, parent);
// move the file to a new directory and rename it
entry.moveTo(parentEntry, "newFile.txt", success, fail);
}
copyTo
------
Copy a file to a new location on the file system. An error results if
the app attempts to:
- copy a file into its parent if a name different from its current one is not provided.
__Parameters:__
- __parent__: The parent directory to which to copy the file. _(DirectoryEntry)_
- __newName__: The new name of the file. Defaults to the current name if unspecified. _(DOMString)_
- __successCallback__: A callback that is passed the new file's `FileEntry` object. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when attempting to copy the file. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function win(entry) {
console.log("New Path: " + entry.fullPath);
}
function fail(error) {
alert(error.code);
}
function copyFile(entry) {
var parent = document.getElementById('parent').value,
parentName = parent.substring(parent.lastIndexOf('/')+1),
parentEntry = new DirectoryEntry(parentName, parent);
// copy the file to a new directory and rename it
entry.copyTo(parentEntry, "file.copy", success, fail);
}
toURL
-----
Returns a URL that can be used to locate the file.
__Quick Example__
// Request the URL for this entry
var fileURL = entry.toURL();
console.log(fileURL);
remove
------
Deletes a file.
__Parameters:__
- __successCallback__: A callback that executes after the file has been deleted. Invoked with no parameters. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when attempting to delete the file. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(entry) {
console.log("Removal succeeded");
}
function fail(error) {
alert('Error removing file: ' + error.code);
}
// remove the file
entry.remove(success, fail);
getParent
---------
Look up the parent `DirectoryEntry` containing the file.
__Parameters:__
- __successCallback__: A callback that is passed the file's parent `DirectoryEntry`. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when attempting to retrieve the parent `DirectoryEntry`. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(parent) {
console.log("Parent Name: " + parent.name);
}
function fail(error) {
alert(error.code);
}
// Get the parent DirectoryEntry
entry.getParent(success, fail);
createWriter
------------
Create a `FileWriter` object associated with the file represented by the `FileEntry`.
__Parameters:__
- __successCallback__: A callback that is passed a `FileWriter` object. _(Function)_
- __errorCallback__: A callback that executes if an error occurs while attempting to create the FileWriter. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(writer) {
writer.write("Some text to the file");
}
function fail(error) {
alert(error.code);
}
// create a FileWriter to write to the file
entry.createWriter(success, fail);
file
----
Return a `File` object that represents the current state of the file
that this `FileEntry` represents.
__Parameters:__
- __successCallback__: A callback that is passed a `File` object. _(Function)_
- __errorCallback__: A callback that executes if an error occurs when creating the `File` object, such as when the file no longer exists. Invoked with a `FileError` object. _(Function)_
__Quick Example__
function success(file) {
console.log("File size: " + file.size);
}
function fail(error) {
alert("Unable to retrieve file properties: " + error.code);
}
// obtain properties of a file
entry.file(success, fail);
|