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