我正在尝试使用 Phonegap[cordova 1.7.0] 在 IOS 上处理文件。 我阅读了如何访问文件并在 API Documentation 上阅读它们电话差距。但我不知道,当文件被读取时,它会被写入哪里? & 如何输出文本、图像或文本包含在 iPhone 屏幕上的任何内容?
这是我使用的代码:
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);
}
最佳答案
从 Cordova 3.5(至少)开始,FileReader
对象只接受 File
对象,不接受 FileEntry
对象(我不确定之前的版本)。
这是一个将本地文件 readme.txt
的内容输出到控制台的示例。这里与 Sana 示例的不同之处在于对 FileEntry.file(...)
的调用。这将提供调用 FileReader.readAs
函数所需的 File
对象。
function readFile() {
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile('readme.txt',
{create: false, exclusive: false}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new window.FileReader();
reader.onloadend = function(evt) {console.log(evt.target.result);};
reader.onerror = function(evt) {console.log(evt.target.result);};
reader.readAsText(file);
}, function(e){console.log(e);});
}, function(e){console.log(e);});
}, function(e) {console.log(e);});
}
关于javascript - 使用 Phonegap 访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10461680/