我正在尝试使用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);
}
答案 0 :(得分:5)
至于Cordova 3.5(至少),FileReader
个对象只接受File
个对象,而不接受FileEntry
个对象(我不确定以前的版本)。
这是一个将本地文件readme.txt
的内容输出到控制台的示例。与Sana的例子不同的是对FileEntry.file(...)
的调用。这将提供调用File
函数所需的FileReader.readAs
对象。
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);});
}
答案 1 :(得分:4)
如果有人需要,这对我有用:
function ReadFile() {
var onSuccess = function (fileEntry) {
var reader = new FileReader();
reader.onloadend = function (evt) {
console.log("read success");
console.log(evt.target.result);
document.getElementById('file_status').innerHTML = evt.target.result;
};
reader.onerror = function (evt) {
console.log("read error");
console.log(evt.target.result);
document.getElementById('file_status').innerHTML = "read error: " + evt.target.error;
};
reader.readAsText(fileEntry); // Use reader.readAsURL to read it as a link not text.
};
console.log("Start getting entry");
getEntry(true, onSuccess, { create: false });
};
答案 2 :(得分:3)
如果您使用的是phonegap(Cordova)1.7.0,则以下页面可在iOS中使用,请在index.html中替换以下模板
<!DOCTYPE html>
<html>
<head>
<title>FileWriter Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample'");
writer.seek(4);
writer.write(" different text");
writer.onwriteend = function(evt){
console.log("contents of file now 'some different text'");
}
};
};
writer.write("some sample text");
}
function fail(error) {
console.log(error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
</html>