我在worklight中使用Cordova文件API创建了一个文件。一旦写入事件完成,我就会读取文件。我尝试在writer.onwriteend事件中编写函数调用readAsText()。但该文件未被阅读。 “独家”选项是否与此有关?一切正常,直到read(),然后没有错误,没有消息..但我认为它不是读取文件。但是,一旦 reader.onloadend事件发生错误:
读错误 { “类型”: “错误”, “泡沫”:假 “cancelBubble”:假, “取消”:假 “lengthComputable”:假, “加载”:0, “总”:0, “目标”:{ “文件名”: “文件:// C:\用户\ IBM_ADMIN \科尔多瓦\文件系统\持久\ RQM \ ExecutionResult.xml”, “readyState的”:2 “结果”:NULL, “错误”:{ “代码”: 1}, “onloadstart”:NULL, “onprogress”:空 “的onload”:NULL, “onabort”:空}}
这是我的代码:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
onFileSystemSuccess, fail);
function onFileSystemSuccess(fileSystem) {
alert("Created File system ");
fileSystem.root.getFile("ExecutionResult.xml", {
create : true,
exclusive : true
}, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.write(JsonData);
writer.onwriteend = function(evt) {
readAfterWriting();
};
}
function readAfterWriting(){
fileSystem.root.getFile("ExecutionResult.xml", {
create : false,
exclusive : false
}, gotFile, fail);
};
}
function gotFile(file) {
readAsText(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);
}
这是发生错误的代码。文件名显示为22。 这是显示的行.. var callbackId = v.id;
window.addEventListener("message", function(e){
_consoleLog("*****PG BC***** " + e.domain + " said: " + e.data);
var r = e.data;
// If a result was returned
_consoleLog("typeof result=" + (typeof r) + " r=" + r);
if (r.length > 0) {
// if ((typeof r == "string") && (r.length > 0)) {
_consoleLog("Result from exec=<" + dumpObj(r, '', ' ', 2) + ">");
eval("var v=" + r + ";");
var callbackId = v.id;
var cast = v.cast;
// var temp = "+cast+"("+this.getJSONString() + ");\n"
if (cast) {
// _consoleLog("CAST==="+"var
// temp="+cast+"("+JSON.stringify(v.message)+");\n");
// eval("var temp="+cast+"("+JSON.stringify(v.message)+");\n");
// _consoleLog("***** CAST:"+" var temp="+cast+"("+r+");");
eval("var temp=" + cast + "(" + r + ");");
v = temp;
}
// _consoleLog("After CAST="+dumpObj(v, '', ' ', 3));
// If status is OK, then return value back to caller
if (v.status === Cordova.callbackStatus.OK) {
// If there is a success callback, then call it now with
// returned value
if ((typeof(Cordova.callbacks[callbackId]) != 'undefined')
&& (Cordova.callbacks[callbackId] != null)) {
if (Cordova.callbacks[callbackId].success) {
try {
Cordova.callbacks[callbackId].success(v.message);
} catch (e) {
_consoleLog("Error in success callback: " + callbackId + " = " + e);
}
// Clear callback if not expecting any more results
if (!v.keepCallback) {
delete Cordova.callbacks[callbackId];
}
}
}
return v.message;
}
答案 0 :(得分:1)
以下是一些符合您期望的代码:
<!DOCTYPE html>
<html>
<head>
<title>FileReader Example</title>
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script type="text/javascript" charset="utf-8">
onWLReady = function() {
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
}
var wlInitOptions = {
onSuccess : onWLReady,
connectOnStartup : false
};
if (window.addEventListener) {
window.addEventListener('load', function() {
WL.Client.init(wlInitOptions); }, false);
} else if (window.attachEvent) {
window.attachEvent('onload', function() {
WL.Client.init(wlInitOptions);
});
}
function log(txt){
var p = document.createElement('p');
document.getElementById("log").appendChild(p);
p.appendChild(document.createTextNode(txt));
}
// PhoneGap is ready
//
function onDeviceReady(){
log("onDeviceReady");
}
function readFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("ExecutionResult.xml", {
create : false, exclusive: false
},
function(fileEntry){
fileEntry.file(function(file) {
readDataUrl(file);
readAsText(file);
},
fail);
},
fail);
}, fail);
}
function writeFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("ExecutionResult.xml", {create: true, exclusive: true},
function(fileEntry) {
fileEntry.createWriter(
function(writer) {
writer.onwriteend = function(evt) {
readFile();
};
writer.onwritestart = function(evt){
log("onwritestart");
};
writer.onwrite = function(evt){
log("onwrite");
};
writer.onerror = function(evt){
log("onerror");
};
writer.write("JSonData");
},
fail);
}
, fail);
},
fail);
}
function readDataUrl(file){
var reader = new FileReader();
log("readDataUrl...");
reader.onloadstart = function(evt){
log("onloadstart: " + evt.target.result);
};
reader.onload = function(evt){
log("onload: " + evt.target.result);
};
reader.onloadend = function(evt){
log("onloadend: " + evt.target.result);
};
reader.onerror = function(evt){
log("onerror: " + evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
var reader = new FileReader();
log("readAsText...");
reader.onloadstart = function(evt){
log("onloadstart: " + evt.target.result);
};
reader.onload = function(evt){
log("onload: " + evt.target.result);
};
reader.onloadend = function(evt){
log("onloadend: " + evt.target.result);
};
reader.onerror = function(evt){
log("onerror: " + evt.target.result);
};
reader.readAsText(file);
}
function fail(error) {
log("fail: "+ evt.target.error.code);
}
</script>
</head>
<body id="content">
<button onclick="writeFile();">Write and read file</button>
<p id="log"></p>
</body>
</html>
希望这会有所帮助......