Cordova PhoneGap 3.0文件读取器不工作
请帮助,我正在使用phoneGap 3.0.0(在IOS上测试)测试一个简单的写/读文件示例 写入工作正常,我可以看到正确写入文件系统文件中的数据,但是出于某种原因,尽管总是返回onloadend,读取还是会返回NULL结果 你知道我做错了什么吗 这是代码。Cordova PhoneGap 3.0文件读取器不工作,cordova,Cordova,请帮助,我正在使用phoneGap 3.0.0(在IOS上测试)测试一个简单的写/读文件示例 写入工作正常,我可以看到正确写入文件系统文件中的数据,但是出于某种原因,尽管总是返回onloadend,读取还是会返回NULL结果 你知道我做错了什么吗 这是代码。 谢谢雷蒙德·卡姆登帮我解决了这个问题。更改为phonegap 3.0.0中的文件API 这是工作版本 传递给readFile的对象是FileEntry对象。为了处理FileEntry的实际文件,可以使用file方法 <!DOCTYPE
谢谢雷蒙德·卡姆登帮我解决了这个问题。更改为phonegap 3.0.0中的文件API 这是工作版本 传递给readFile的对象是FileEntry对象。为了处理FileEntry的实际文件,可以使用file方法
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
<title>Open File</title>
<script type="text/javascript" src="js/xui-2.3.2.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" >
var fileObject;
document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileObject = fileEntry;
x$('#saveFile_btn').on('click', function() {
saveFileContent();
});
}
function gotFileWriter(writer) {
var myText = document.getElementById('my_text').value;
/* prepare write listeners */
writer.onwriteend = function(evt) {
console.log("DONE Writing text");
x$('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');
fileObject.file(function(e) {
console.log("called the file func on the file ob");
var reader = new FileReader();
/* prepare read listeners */
reader.onloadstart = function(evt) {
console.log("started reading");
};
reader.onabort = function(evt) {
console.log("aborted read text");
console.log(evt.target.result);
x$('#contents').html('<strong>Aborted</strong> <br />');
};
reader.onerror = function(evt) {
console.log("Error read text");
console.log("Error"+evt.error.code);
x$('#contents').html('<strong>Error:</strong> <br />' + evt.error.code);
};
reader.onloadend = function(evt) {
console.log("successfully read text");
console.log("Target Result ["+evt.target.result+"]");
console.log("Reader Result ["+reader.result+"]");
x$('#contents').html('<strong>File contents:</strong> <br />' + evt.target.result);
};
console.log("Reading text");
reader.readAsText(e);
});
};
console.log("Writing text ["+myText+"]");
writer.write(myText);
}
function saveFileContent() {
fileObject.createWriter(gotFileWriter, fail);
}
function fail(error) {
alert(error.code);
}
</script>
</head>
<body>
<input type="text" id="my_text" />
<input type="button" id="saveFile_btn" value="Save" />
<div id="message"></div>
<div id="contents"></div>
</body>
</html>
打开文件
var文件对象;
文件。添加的监听器(“deviceready”,onDeviceReady,true);
函数ondevicerady(){
requestFileSystem(LocalFileSystem.PERSISTENT,0,onfileSystemsSuccess,fail);
}
函数onFileSystemsSuccess(文件系统){
getFile(“readme.txt”,{create:true,exclusive:false},gotFileEntry,fail);
}
函数gotFileEntry(fileEntry){
fileObject=fileEntry;
x$('saveFile'u btn')。在('click',function()上{
saveFileContent();
});
}
函数gotFileWriter(writer){
var myText=document.getElementById('my_text').value;
/*准备编写侦听器*/
writer.onwriteend=函数(evt){
console.log(“完成编写文本”);
x$('#message').html('文件内容已写入。
文件路径:'+fileObject.fullPath+'');
file(函数(e){
log(“调用文件ob上的文件func”);
var reader=new FileReader();
/*准备阅读听者*/
reader.onloadstart=函数(evt){
log(“开始读取”);
};
reader.onabort=函数(evt){
log(“中止读取文本”);
日志(evt.target.result);
x$('#contents').html('中止
');
};
reader.onerror=函数(evt){
日志(“错误读取文本”);
日志(“错误”+evt.Error.code);
x$('#contents').html('错误:
'+evt.Error.code);
};
reader.onloadend=函数(evt){
log(“成功读取文本”);
log(“Target Result[“+evt.Target.Result+”);
console.log(“Reader Result[“+Reader.Result+”]);
x$('#contents').html('文件内容:
'+evt.target.result);
};
控制台日志(“阅读文本”);
reader.readAsText(e);
});
};
log(“写入文本[“+myText+”]”);
writer.write(myText);
}
函数saveFileContent(){
createWriter(gotFileWriter,失败);
}
功能失败(错误){
警报(错误代码);
}
Hi Mark,我遇到了类似的问题,无法解决。你介意看一下这里吗?
2013-08-22 13:55:22.215 Chapter2[33715:c07] Writing text [sdsd]
2013-08-22 13:55:22.219 Chapter2[33715:c07] DONE Writing text
2013-08-22 13:55:22.219 Chapter2[33715:c07] Reading text
2013-08-22 13:55:22.220 Chapter2[33715:c07] started reading
2013-08-22 13:55:22.221 Chapter2[33715:c07] successfully read text
2013-08-22 13:55:22.222 Chapter2[33715:c07] Target Result []
2013-08-22 13:55:22.222 Chapter2[33715:c07] Reader Result []
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
<title>Open File</title>
<script type="text/javascript" src="js/xui-2.3.2.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" >
var fileObject;
document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileObject = fileEntry;
x$('#saveFile_btn').on('click', function() {
saveFileContent();
});
}
function gotFileWriter(writer) {
var myText = document.getElementById('my_text').value;
/* prepare write listeners */
writer.onwriteend = function(evt) {
console.log("DONE Writing text");
x$('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');
fileObject.file(function(e) {
console.log("called the file func on the file ob");
var reader = new FileReader();
/* prepare read listeners */
reader.onloadstart = function(evt) {
console.log("started reading");
};
reader.onabort = function(evt) {
console.log("aborted read text");
console.log(evt.target.result);
x$('#contents').html('<strong>Aborted</strong> <br />');
};
reader.onerror = function(evt) {
console.log("Error read text");
console.log("Error"+evt.error.code);
x$('#contents').html('<strong>Error:</strong> <br />' + evt.error.code);
};
reader.onloadend = function(evt) {
console.log("successfully read text");
console.log("Target Result ["+evt.target.result+"]");
console.log("Reader Result ["+reader.result+"]");
x$('#contents').html('<strong>File contents:</strong> <br />' + evt.target.result);
};
console.log("Reading text");
reader.readAsText(e);
});
};
console.log("Writing text ["+myText+"]");
writer.write(myText);
}
function saveFileContent() {
fileObject.createWriter(gotFileWriter, fail);
}
function fail(error) {
alert(error.code);
}
</script>
</head>
<body>
<input type="text" id="my_text" />
<input type="button" id="saveFile_btn" value="Save" />
<div id="message"></div>
<div id="contents"></div>
</body>
</html>