【问题标题】:Chrome.fileSystem store data in fileChrome.fileSystem 将数据存储在文件中
【发布时间】:2015-06-23 21:06:47
【问题描述】:

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

任何指导或建议都会很棒! 谢谢 提供

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2015-08-24
      • 2016-08-20
      • 2018-02-21
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多