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.
This object represents a file on a file system. It is defined in the W3C Directories and Systems specification.
NOTE: The following attributes are defined by the W3C specification, but are not supported by Cordova:
Look up metadata about a file.
Parameters:
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);
Set metadata on a file. Only works on iOS currently - this will set the extended attributes of a file.
Parameters:
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
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);
Move a file to a different location on the file system. It is an error to attempt to:
In addition, an attempt to move a file on top of an existing file must attempt to delete and replace that file.
Parameters:
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); }
Copy a file to a new location on the file system. It is an error to attempt to:
Parameters:
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); }
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);
Deletes a file.
Parameters:
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);
Look up the parent DirectoryEntry containing the file.
Parameters:
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);
Create a FileWriter object associated with the file that the FileEntry represents.
Parameters:
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);
Return a File object that represents the current state of the file that this FileEntry represents.
Parameters:
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);