a.stringham
a.stringham

Reputation: 475

Unable to write to file with phonegap

Using the methods found on the phonegap api I'm trying to write to a file. This works in Android, but on an iOS device the writer is returning an error. Whenever I call writeFile() it returns an error, and the param passed into writeFail is -1. I cannot see why -1 is being passed into the error function, or why it's even failing to begin with. Has anyone else used the fileWriter on an iOS device, or can you see what I might be doing wrong?

function writeFile() {
    var paths = navigator.fileMgr.getRootPaths();
    var writer = new FileWriter(paths[0] + "write.txt");
    writer.onwrite = writeSuccess;
    writer.onerror = writeFail;

    writer.write("some sample text");
    // The file is now 'some sample text'
}

function writeSuccess() {
    console.log("Write has succeeded");
}

function writeFail(evt) {
    console.log(evt);
    console.log(evt.target.error.code);
}

Upvotes: 3

Views: 7027

Answers (2)

Rodrigo Dias
Rodrigo Dias

Reputation: 1340

If you want to write to a file this is the function(phonegap 2.5)

function fileWrite(filePath, text) {
  var onFSWin = function(fileSystem) {
    fileSystem.root.getFile(filePath, {create: true, exclusive: false}, onGetFileWin, onFSFail);
  }
  var onGetFileWin = function(fileEntry) {
    fileEntry.createWriter(gotFileWriter, onFSFail);
  }
  var gotFileWriter = function(writer) {
    writer.write(text);
  }
  var onFSFail = function(error) {
   console.log(error.code);
  }
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin, onFSFail);
 }  

Upvotes: 2

Andreas Schipplock
Andreas Schipplock

Reputation: 39

I had the same problem but I crawled through the mailing list and finally found the solution:

var writer = new FileWriter("write.txt");

This is it. Simply don't prepend the "Documents"-path. The documentation is wrong on that (still).

And don't forget to not use "readAsDataURL" as it would silently not work (on iOS). Hope I could help you.

Upvotes: 2

Related Questions