我正在使用HTML5的FileSystem API和Worklight一起创建缓存文件JSON格式,我创建的文件没有任何问题,问题是当我尝试向创建的文件添加一些文本时,我使用了fileWriter但是它没有写任何事情。
这是我开发的更新功能:
function update(fileName){
function onInitFs(fs) {
fs.root.getFile(fileName, {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry .
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write it to log.txt.
var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
document.querySelector('#example-list-fs-ul').innerHTML = 'Error: ' + msg;
}
}
这是显示文件内容的read函数,它返回一个空字符串:
function read(fileName){
function onInitFs(fs) {
fs.root.getFile(fileName, {}, function(fileEntry) {
// Get a File object representing the file,
// then use FileReader to read its contents.
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
alert(this.result);
};
reader.readAsText(file);
}, errorHandler);
}, errorHandler);
}
window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);
}
任何人都有任何想法如何解决这个问题以及如何成功地写入和读取文件? 谢谢
答案 0 :(得分:1)
我认为最好使用专为您要实现的目的而设计的一些Worklight功能,例如:
答案 1 :(得分:0)
这是一个允许创建/编辑/读取/删除简单文本文件的示例。 希望这会有所帮助。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>CordovaApp</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<!--
<link rel="shortcut icon" href="images/favicon.png">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
-->
<link rel="stylesheet" href="css/main.css">
<script>window.$ = window.jQuery = WLJQ;</script>
<script type="text/javascript" charset="utf-8">
/**
* Function called when page has finished loading.
*/
function init(){
console.log("onDeviceReady");
}
onWLReady = function() {
// Wait for PhoneGap to load
document.addEventListener("deviceready", init, false);
}
function createFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false},
function(fileEntry) {
fileEntry.createWriter(
function(writer) {
writer.onwriteend = function(evt) {
log("File created...");
};
writer.onwritestart = function(evt){
log("onwritestart");
};
writer.onwrite = function(evt){
log("onwrite");
};
writer.onerror = function(evt){
log("onerror");
};
writer.write("File created at: " + new Date().toLocaleString());
},
fail);
}
, fail);
},
fail);
}
function editFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: false, exclusive: false},
function(fileEntry) {
fileEntry.createWriter(
function(writer) {
writer.onwriteend = function(evt) {
log("File edited");
};
writer.onwritestart = function(evt){
log("onwritestart");
};
writer.onwrite = function(evt){
log("onwrite");
};
writer.onerror = function(evt){
log("onerror");
};
writer.seek(writer.length);
writer.write("<br>File edited at: " + new Date().toLocaleString());
},
fail);
}
, fail);
},
fail);
}
function readFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("readme.txt", {
create : false
},
function(fileEntry){
fileEntry.file(function(file) {
readDataUrl(file);
readAsText(file);
},
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);
reader.abort();
}
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 deleteFile() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getFile("readme.txt", {
create : false
},
function(fileEntry){
fileEntry.remove(function() {log("File deleted.");
},
fail);
},
fail);
}, fail);
}
function fail(error){
var msg = error;
switch(error.code)
{
case FileError.NOT_FOUND_ERR:
msg = "File Not Found";
break;
case FileError.SECURITY_ERR:
msg = "Security Error";
break;
case FileError.ABORT_ERR:
msg = "Abort error";
break;
case FileError.NOT_READABLE_ERR:
msg = "Not Readable";
break;
case FileError.ENCODING_ERR:
msg = "Encoding Error";
break;
case FileError.NO_MODIFICATION_ALLOWED_ERR:
msg = "No Modification Allowed";
break;
case FileError.INVALID_STATE_ERR:
msg = "Invalid State";
break;
case FileError.SYNTAX_ERR:
msg = "Syntax Error";
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = "Invalid Modification Error";
break;
case FileError.QUOTA_EXCEEDED_ERR:
msg = "Quota Exceeded";
break;
case FileError.TYPE_MISMATCH_ERR:
msg = "Type Mismatch Error";
break;
case FileError.PATH_EXISTS_ERR:
msg = "Path Already Exists Error";
break;
}
log("fail: "+ msg);
}
function log(info){
document.getElementById('log').innerHTML += "<br>" + info;
}
function clearLog(info){
document.getElementById('log').innerHTML = "";
}
</script>
</head>
<body style="display: none;">
<!--application UI goes here-->
<div>
<button onclick="createFile();">Create File</button><br>
<button onclick="editFile();">Edit File</button><br>
<button onclick="readFile();">Read File</button><br>
<button onclick="deleteFile();">Delete File</button><br>
<button onclick="clearLog();">Clear Log</button><br>
</div>
<script src="js/initOptions.js"></script>
<script src="js/main.js"></script>
<script src="js/messages.js"></script>
<div><u><b>Log:</b></u></div>
<div id=log></div>
</body>
</html>
请注意,Javascript方法最好是在 js / main.js 中,而不是在html文件中定义。我简单地将工作样本作为单个文件提供。