1

我正在尝试使用 chrome.fileSystem api 将从 Chrome 地理定位 api 收集的数据保存到一个 txt 文件中,但是虽然我能够为此目的创建一个文件,但该文件在过程结束时仍然是空的。为简单起见,我首先尝试在文件中添加一个字符串 (1234567890)。正如您在代码中看到的那样,我添加了几个 console.log 来调查代码中的哪些行不执行,并且似乎代码没有从 writableFileEntry.creatrwriter() 向下执行。

chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
   console.log('writableFileEntry - here');
    writableFileEntry.createWriter(function(writer) {
      console.log('After writableFileEntry - here');
      writer.onerror = errorHandler;
      writer.onwriteend = function(e) {
        console.log('write complete');
      };
       console.log('before Blob - here');
      writer.write(new Blob(['1234567890'], {type: 'text/plain'}));
    }, errorHandler);
});

manifest.json 文件如下所示:

"permissions": ["location","geolocation", "storage", "unlimitedStorage", "fileSystem", "syncFileSystem", {"fileSystem": ["write"]}
          ],
  "app": {
    "background": {
      "scripts": ["background.js"]
        }
      },
  "sockets": {
        "tcp": {
            "connect": "localhost:80"
        },
        "udp": {
             "send": "localhost:80"
        }
      }

任何指导或建议都会很棒!谢谢奥弗

4

1 回答 1

0

在使用谷歌文档中的一些代码后,我遇到了同样的问题。该代码引用了一个名为 errorHandler 的函数,该函数不存在。添加一个名为 errorHandler() 的函数,它应该可以工作。

像这样的东西:

chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {

        function errorHandler(){
            console.log('error');
        };

        writableFileEntry.createWriter(function(writer) {
          writer.onerror = errorHandler;
          writer.onwriteend = function(e) {
            console.log('write complete');
          };
          writer.write(new Blob(['1234567890'], {type: 'text/plain'}));
        }, errorHandler);

    });
于 2015-10-25T22:37:54.073 回答