错误原因是什么?”“正在运行fileSystem.chooseEntry:调用页面无效。无法从后台页面调用此函数。”

时间:2018-11-12 10:17:13

标签: google-chrome google-chrome-app

我使用chrome.fileSystem.chooseEntry API将文件保存到USB驱动器。

window.chrome.fileSystem.chooseEntry({ type: 'saveFile' }, function (writableEntry) {
  let blob = new Blob([message.params.data], { type: 'text/plain' });
  if (!writableEntry) {
    return;
  }
  writableEntry.createWriter(function (writer) {
    writer.onerror = errorHandler;
    writer.onwriteend = () => {
      console.log('write complete!');
    };
    if (blob) {
      writer.truncate(blob.size);
      waitForIO(writer, function() {
        writer.seek(0);
        writer.write(blob);
      });
    }
  });
});

function waitForIO(writer, callback) {
    // set a watchdog to avoid eventual locking:
    var start = Date.now();
    // wait for a few seconds
    var reentrant = function () {
        if (writer.readyState === writer.WRITING && Date.now() - start < 4000) {
            setTimeout(reentrant, 100);
            return;
        }
        if (writer.readyState === writer.WRITING) {
            console.error(`Write operation taking too long, aborting! (current writer readyState is ${writer.readyState})`);
            writer.abort();
        }
        else {
            callback();
        }
    };
    setTimeout(reentrant, 100);
}

回调返回不是fileEntry.writableEntry是未定义的。你知道是什么问题吗?

0 个答案:

没有答案