标签:
本文将介绍PhoneGap API——File:通过JavaScript截获本地文件系统。File是用于读取、写入和浏览文件系统层次结构的PhoneGap API。
对象:
属性:
备注:以下属性虽然在W3C规范中进行了定义,但PhoneGap并不支持:
方法:
支持的平台:
getMetadata:
参数:
简单的范例:
function success(metadata) { console.log("Last Modified:" + metadata.modificationTime); } function fail(error) { alert(error.code); } // 请求这个条目的元数据对象 entry.getMetadata(success, fail);
moveTo:
参数:
简单的范例:
function success(entry) { console.log("New Path:" + entry.fullPath); } function fail(error) { alert(error.code); } function moveDir(entry) { var parent = document.getElementById(‘parent‘).value; newName = document.getElementById(‘newName‘).value; parentEntry = new DirectoryEntry({fullPath: parent}); // 移动目录到一个新目录,并将其重命名 entry.moveTo(parentEntry, newName, success, fail); }
copyTo:
参数:
简单的范例:
function win(entry) { console.log("New Path:" + entry.fullPath); } function fail(error) { alert(error.code); } function copyDir(entry) { var parent = document.getElementById(‘parent‘).value, newName = document.getElementById(‘newName‘).value, parentEntry = new DirectoryEntry({fullPath: parent}); // 拷贝目录到一个新的目录,并将其重命名 entry.copyTo(parentEntry, newName, success, fail); }
toURI:
简单的范例:
// 请求此条目的URI var uri = entry.toURI(); console.log(uri);
remove:
参数:
简单的范例:
function success(entry) { console.log("Removal succeeded"); } function fail(error) { alert(‘Error removing directory: ‘ + error.code); } // 移除这个目录 entry.remove(success, fail);
getParent:
参数:
简单的范例:
function success(parent) { console.log("Parent Name: " + parent.name); } function fail(error) { alert(‘Failed to get parent directory: ‘ + error.code); } // 获得父级DirectoryEntry对象 entry.getParent(success, fail);
createReader:
简单的范例:
getDirectory:
参数:
简单的范例:
function success(parent) { console.log("Parent Name:" + parent.name); } function fail(error) { alert("Unable to create new directory:"+ error.code); } // 检索一个已存在的目录,如果该目录不存在时则创建该目录 entry.getDirectory(“newDir”, {create: true, exclusive: false}, success, fail);
getFile:
参数:
简单的范例:
function success(parent) { console.log("Parent Name:" + parent.name); } function fail(error) { alert("Failed to retrieve file:" + error.code); } // 检索一个已存在的文件,如果该文件不存在时则创建该文件 entry.getFile(“newFile.txt”, {create: true, exclusive: false}, success, fail);
removeRecursively:
参数:
简单的范例:
console.log("Remove Recursively Succeeded"); } function fail(error) { alert("Failed to remove directory or it‘s contents:" + error.code); } // 删除此目录及其所有内容 entry.removeRecursively(success, fail);
方法:
支持的平台:
readEntries:
参数:
简单的范例:
function success(entries) { var i; for(i=0;i< entries.length;i++) { console.log(entries[i].name); } } function fail(error) { alert("Failed to list directory contents:" + error.code); } // 创建一个目录读取器 var directoryReader = dirEntry.createReader(); // 获取目录中的所有条目 directoryReader.readEntries(success,fail);
属性:
详述:
支持的平台:
属性:
详述:
支持的平台:
File System简单的范例:
function onSuccess(fileSystem) { console.log(fileSystem.name); console.log(fileSystem.root.name); } // 请求当前持久化的文件系统 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
完整的范例:
<!DOCTYPE html> <html> <head> <title>File System Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); } function onFileSystemSuccess(fileSystem) { console.log(fileSystem.name); console.log(fileSystem.root.name); } function fail(evt) { console.log(evt.target.error.code); } </script> </head> <body> <h1>Example</h1> <p>File System</p> </body> </html>
属性:
备注:以下属性虽然在W3C规范中进行了定义,但PhoneGap并不支持:
方法:
支持的平台:
getMetadata:
参数:
简单的范例:
function success(metadata) { console.log("Last Modified:" + metadata.modificationTime); } function fail(error) { alert(error.code); } // 请求这个条目的元数据对象 entry.getMetadata(success, fail);
moveTo:
参数:
简单的范例:
function success(entry) { console.log("New Path: " + entry.fullPath); } function fail(error) { alert(error.code); } function moveFile(entry) { var parent = document.getElementById(‘parent‘).value, parentEntry = new DirectoryEntry({fullPath: parent}); // 移动文件到一个新目录,并将其重命名 entry.moveTo(parentEntry, "newFile.txt", success, fail); }
copyTo:
参数:
简单的范例:
function win(entry) { console.log("New Path: " + entry.fullPath); } function fail(error) { alert(error.code); } function copyFile(entry) { var parent = document.getElementById(‘parent‘).value, parentEntry = new DirectoryEntry({fullPath: parent}); // 拷贝文件到一个新的目录,并将其重命名 entry.copyTo(parentEntry, "file.copy", success, fail); }
toURI:
简单的范例:
// 请求此条目的URI var uri = entry.toURI(); console.log(uri);
remove:
参数:
简单的范例:
function success(entry) { console.log("Removal succeeded"); } function fail(error) { alert(‘Error removing file: ‘ + error.code); } // 移除该文件 entry.remove(success, fail);
getParent:
参数:
简单的范例:
function success(parent) { console.log("Parent Name: " + parent.name); } function fail(error) { alert(error.code); } // 获得父级DirectoryEntry对象 entry.getParent(success, fail);
createWriter:
参数:
简单的范例:
function success(writer) { writer.write("Some text to the file"); } function fail(error) { alert(error.code); } // 创建一个用于写文件的FileWriter对象 entry.createWriter(success, fail);
file
参数:
简单的范例:
function success(file) { console.log("File size: " + file.size); } function fail(error) { alert("Unable to retrieve file properties: " + error.code); } // 获得此文件的属性 entry.file(success, fail);
属性:
方法:
详述:
支持的平台:
ReadAsDataURL:
参数:
简单的范例:
unction win(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("read success"); console.log(evt.target.result); }; reader.readAsDataURL(file); } var fail = function(evt) { console.log(error.code); }; entry.file(win, fail);
ReadAsText:
参数:
简单的范例:
function win(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("read success"); console.log(evt.target.result); }; reader.readAsText(file); } var fail = function(evt) { console.log(error.code); }; entry.file(win, fail);
Abort 简单的范例:
function win(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("read success"); console.log(evt.target.result); }; reader.readAsText(file); reader.abort(); } function fail(error) { console.log(error.code); } entry.file(win, fail);
完整的范例:
<!DOCTYPE html> <html> <head> <title>FileReader Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } function gotFS(fileSystem) { fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.file(gotFile, fail); } function gotFile(file){ readDataUrl(file); readAsText(file); } function readDataUrl(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as data URL"); console.log(evt.target.result); }; reader.readAsDataURL(file); } function readAsText(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as text"); console.log(evt.target.result); }; reader.readAsText(file); } function fail(evt) { console.log(evt.target.error.code); } </script> </head> <body> <h1>Example</h1> <p>Read File</p> </body> </html>
iOS 的特异情况:
属性:
方法:
详述:
支持的平台:
Seek的简单范例:
function win(writer) { // 快速将文件指针指向文件的尾部 writer.seek(writer.length); }; var fail = function(evt) { console.log(error.code); }; entry.createWriter(win, fail);
Truncate 的简单范例:
function win(writer) { writer.truncate(10); } var fail = function(evt) { console.log(error.code); } entry.createWriter(win, fail);
Write 的简单范例:
function win(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.write("some sample text"); }; var fail = function(evt) { console.log(error.code); }; entry.createWriter(win, fail);
Append 的简单范例:
function win(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.seek(writer.length); writer.write("appended text); } var fail = function(evt) { console.log(error.code); }; entry.createWriter(win, fail); Abort 的简单范例: function win(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.write("some sample text"); writer.abort(); } var fail = function(evt) { console.log(error.code); }; entry.createWriter(win, fail);
完整的范例:
<!DOCTYPE html> <html> <head> <title>FileWriter Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.0.9.4.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } function gotFS(fileSystem) { fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.createWriter(gotFileWriter, fail); } function gotFileWriter(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.write("some sample text"); // 文件当前内容是“some sample text” writer.truncate(11); // 文件当前内容是“some sample” writer.seek(4); // 文件当前内容依然是“some sample”,但是文件的指针位于“some”的“e”之后 writer.write(" different text"); // 文件的当前内容是“some different text” } function fail(error) { console.log(error.code); } </script> </head> <body> <h1>Example</h1> <p>Write File</p> </body> </html>
属性:
常量:
说明:
属性:
方法:
详述:
支持的平台:
简单的范例:
// !! 假设变量fileURI包含一个指向设备上一个文本文件的有效URI var win = function(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); } var fail = function(error) { alert("An error has occurred: Code = " = error.code); } var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=fileURI.substr(fileURI.lastIndexOf(‘/‘)+1); options.mimeType="text/plain"; var params = new Object(); params.value1 = "test"; params.value2 = "param"; options.params = params; var ft = new FileTransfer(); ft.upload(fileURI, "http://some.server.com/upload.php", win, fail, options);
完整的范例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>File Transfer Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.0.9.4.min.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完成 function onDeviceReady() { // 从指定来源检索图像文件位置 navigator.camera.getPicture(uploadPhoto, function(message) { alert(‘get picture failed‘); }, { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY } ); } function uploadPhoto(imageURI) { var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=imageURI.substr(imageURI.lastIndexOf(‘/‘)+1); options.mimeType="image/jpeg"; var params = new Object(); params.value1 = "test"; params.value2 = "param"; options.params = params; var ft = new FileTransfer(); ft.upload(imageURI, "http://some.server.com/upload.php", win, fail, options); } function win(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); } function fail(error) { alert("An error has occurred: Code = " = error.code); } </script> </head> <body> <h1>Example</h1> <p>Upload File</p> </body> </html>
属性:
说明:
属性:
说明:
iOS的特异情况:
属性:
常量:
说明:
属性:
支持的平台:
简单的范例:
// 获取data目录,如果不存在则创建该目录。 dataDir = fileSystem.root.getDirectory("data", {create: true}); // 创建lockfile.txt,当且仅当该文件不存在时。 lockFile = dataDir.getFile("lockfile.txt", {create: true, exclusive: true});
方法:
常量:
详述:
支持的平台:
Request File System简单的范例:
function onSuccess(fileSystem) { console.log(fileSystem.name); } // 请求持久化的文件系统 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError); Resolve Local File System URI的简单范例: function onSuccess(fileEntry) { console.log(fileEntry.name); } window.resolveLocalFileSystemURI("file:///example.txt", onSuccess, onError);
完整的范例:
<!DOCTYPE html> <html> <head> <title>Local File System Example</title> <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8"> // 等待加载PhoneGap document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap加载完毕 function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); window.resolveLocalFileSystemURI("file:///example.txt", onResolveSuccess, fail); } function onFileSystemSuccess(fileSystem) { console.log(fileSystem.name); } function onResolveSuccess(fileEntry) { console.log(fileEntry.name); } function fail(evt) { console.log(evt.target.error.code); } </script> </head> <body> <h1>Example</h1> <p>Local File System</p> </body> </html>
属性:
详述:
支持的平台:
简单的范例:
function win(metadata) { console.log("Last Modified: " + metadata.modificationTime); } // 请求此条目的metadata对象 entry.getMetadata(win, null);
标签:
原文地址:http://www.cnblogs.com/tianyalu/p/4608109.html