无法用phonegap对文件进行写操作

时间:2022-08-04 23:06:35

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?

使用在phonegap api上找到的方法,我试图将其写入文件。这在Android上是可行的,但是在iOS设备上,作者返回了一个错误。每当我调用writeFile()时,它都会返回一个错误,传入writeFail的参数为-1。我不明白为什么-1被传递到错误函数中,或者为什么它一开始就失败了。有人在iOS设备上使用fileWriter吗?或者你能看到我做错了什么吗?

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);
}

2 个解决方案

#1


2  

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.

而且不要忘记不要使用“readAsDataURL”,因为它会悄无声息地(在iOS上)不工作。希望我能帮助你。

#2


2  

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

如果要写入文件,这是函数(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);
 }  

#1


2  

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.

而且不要忘记不要使用“readAsDataURL”,因为它会悄无声息地(在iOS上)不工作。希望我能帮助你。

#2


2  

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

如果要写入文件,这是函数(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);
 }