javascript - Chrome.fileSystem 将数据存储在文件中

标签 javascript google-chrome web-applications google-chrome-storage

我正在尝试使用 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"
        }
      }

任何指导或建议都会很棒! 谢谢 报价

最佳答案

在使用谷歌文档中的一些代码后,我刚刚遇到了同样的问题。该代码引用了一个名为 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);

    });

关于javascript - Chrome.fileSystem 将数据存储在文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013543/

相关文章:

css - Chrome float 正确调整大小错误?

google-apps-script - iFrame 中的应用程序脚本 HTMLService 应用程序

javascript - 集成 React.js 非 JS 服务器 API

javascript - 如何获取相对于 HTML 页面的标记位置

php - 提交按钮可以在不刷新 AJAX 的情况下工作吗?

javascript - Shift-双击时触发 JavaScript Action

javascript - 在 google chrome 上下载大文件(最大 15 mb)时出现问题

java - Tomcat 中的共享 JNI 库 (.so) - UnsatisfiedLinkError

javascript - graphqljs - 如何搜索枚举类型

javascript - 将变量设置为 'undefined' 实际上会释放空间吗?