<html>
<head>
<title>FileWriter Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8">
funtion init()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
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);
fileEntry.createWriter(win, fail);
fileEntry.file(gotFile, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
writer.truncate(100);
writer.onwriteend = function(evt) {
writer.seek(4);
writer.write(" different text +");
writer.onwriteend = function(evt){
writer.write("contents of file now 'sihesh'");
}
};
};
}
function gotFile(file){
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert("Read as text");
alert(evt.target.result);
};
reader.readAsText(file);
alert("success");
}
function win(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.seek(writer.length);
writer.write("appended text");
alert("Append hogya na baa");
};
var fail = function(evt) {
console.log(error.code);
};
function fail(evt) {
console.log(evt.target.error.code);
}
function fail(error) {
console.log(error.code);
}
</script>
</head>
<body onload="init()">
<input type="button" id="b1" value="Write" onClick="gotFileWriter()"/><br>
<input type="button" id="b2" value="Read" onClick="readAsText()" /><br>
<input type="button" id="b3" value="Append"/><br>
<input type="button" id="b4" value="Delete"/><br>
<p>Write File</p>
</body>
</html>
基本上我想要的是点击写入按钮它应该在文件中写入内容并且点击阅读按钮它应该检索并显示它但是发生了什么是因为设备准备好了这些功能被一个接一个地自动调用所以那里没有使用按钮上的点击方法,但我想要的是点击它应该写的写按钮,点击它应阅读的阅读按钮。
所以请任何人帮我解决我的问题。 :(
注意:当我在click for writer上传递gotFileWriter()方法(没有设备就绪)时,它会给出以下错误。 09-30 00:16:10.112:E / Web控制台(829):TypeError:表达式结果&#39;文件&#39; [undefined]不是对象。在file:///android_asset/www/cordova-1.9.0.js:2404
答案 0 :(得分:0)
我解决了我的问题。以下是我的代码
<html>
<head>
<title>FileWriter Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8">
var file;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
file=fileSystem;
document.querySelector("#b1").addEventListener("click",read1);
document.querySelector("#b2").addEventListener("click",write1);
document.querySelector("#b3").addEventListener("click",append1);
document.querySelector("#b4").addEventListener("click",delete1);
}
function read1()
{
file.root.getFile("mca3b.txt", {create: true, exclusive: false}, readAsText, fail);
}
function write1()
{
file.root.getFile("mca3b.txt", {create: true, exclusive: false}, gotFileWriter, fail);
}
function append1()
{
file.root.getFile("mca3b.txt", {create: true, exclusive: false}, win, fail1);
}
function delete1()
{
file.root.getFile("mca3b.txt", {create: true, exclusive: false}, del_success, del_fail);
}
function gotFileWriter(writer) {
var mm=document.getElementById("t1").value;
writer.createWriter(function(cc)
{
cc.onwrite=function()
{
alert("Done writing");
}
cc.write(mm);
});
alert("WRiterrrrr");
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert("Read as text");
alert(evt.target.result);
};
reader.readAsText(file);
alert("success");
}
function win(writer) {
writer.createWriter(function(cc)
{
cc.onwrite=function()
{
alert("Done writing");
}
cc.seek(cc.length);
cc.write("This is appended text");
});
alert("WRiterrrrr");
alert("Append hogya na baa");
};
var fail = function(evt)
{
console.log(error.code);
};
function fail(evt) {
console.log(evt.target.error.code);
}
function fail1(error) {
console.log(error.code);
}
function del_success(entry) {
entry.remove(success20,fail20);
}
function success20()
{
alert("Removal succeeded");
}
function fail20()
{
alert("Error while deleting file");
}
function del_fail(error) {
alert('Error removing file: ' + error.code);
}
</script>
</head>
<body>
<center><h1>MCA3B</h1></center>
<p>Read,Write ,Append and Delete of File</p>
<input type="text" placeholder="Enter Some Text" id="t1"/><br>
<input type="button" id="b2" value="Write"/><br>
<input type="button" id="b1" value="Read"/><br>
<input type="button" id="b3" value="Append"/><br>
<input type="button" id="b4" value="Delete"/>
</body>
</html>