我使用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是未定义的。你知道是什么问题吗?