В одном проекте мне удалось настроить запись файла с помощью функции phonegap:
$("#download").live("click", function(){
writeFile();
});
function writeFile(){
$.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
json = result;
removeHTMLTags();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
});
}
function removeHTMLTags(){
if(json){
var strInputCode = json;
/*
This line is optional, it replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with >
*/
strInputCode = strInputCode.replace(/( )*/g,"");
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
// strTagStrippedText = '"';
strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
//strTagStrippedText += '"';
}};
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write(strTagStrippedText);
readFile();
// contents of file now 'some sample text'
// writer.truncate(11);
// contents of file now 'some sample'
// writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
//writer.write(" different text");
// contents of file now 'some different text'
}
// Einde FILE WRITER
function fail(error) {
console.log(error.code);
}
В другом проекте я делаю то же самое, и это не сработает, я создал другой проект, потому что мне нужно было начинать все сначала. Я делаю это так:
$(".startNew").live("click", function(){
writeFile();
$.mobile.changePage($("#games"));
krijgSpellen();
});
function writeFile(){
navigator.notification.alert("write file");
$.get("http://www.bartdekimpe.be/anoire/index.php/admin/getGamesUserJson/34", function(result){
json = result;
removeHTMLTags();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
});
}
function removeHTMLTags(){
if(json){
var strInputCode = json;
/*
This line is optional, it replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with >
*/
strInputCode = strInputCode.replace(/( )*/g,"");
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
// strTagStrippedText = '"';
strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
strTagStrippedText = $.trim(strTagStrippedText); // overbodige spaties weghalen
//strTagStrippedText += '"';
}};
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write(strTagStrippedText);
readFile();
// contents of file now 'some sample text'
// writer.truncate(11);
// contents of file now 'some sample'
// writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
//writer.write(" different text");
// contents of file now 'some different text'
}
// Einde FILE WRITER
function fail(error) {
console.log(error.code);
}
Это не будет записывать файл, действительно странно, когда я сделаю это:
$(".startNew").live("click", function(){
writeFile();
$.mobile.changePage($("#games"));
krijgSpellen();
});
файл не записывается, но когда я делаю это:
$(".startNew").live("click", function(){
writeFile();
});
Работает и пишет файл