我正在尝试读取已在本地计算机中创建的文本文件(log.txt),并希望使用文件系统 api 方法将文本附加到来自 webworker 的同一文件,但无法这样做,也无法获得任何错误即使尝试使用 react app 有线应用程序通过 postMessage() 方法将错误发送到主线程,也会记录日志
//worker.js
*/* eslint no-restricted-globals: 0 */
// eslint-disable-next-line no-restricted-globals
self.requestFileSystemSync = self.webkitRequestFileSystemSync || self.requestFileSystemSync;
// eslint-disable-next-line no-restricted-globals
self.BlobBuilder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder;
try {
var fs = self.requestFileSystemSync(self.TEMPORARY, 1024 * 1024, onFs, onError);
} catch (e) {
onError(e);
}
function onFs(fs) {
fs.root.getFile('/Downloads/log.txt', {}, function(fileEntry) {
// Obtain the File object representing the FileEntry.
// Use FileReader to read its contents.
fileEntry.file(function(file) {
var reader = new FileReader();
postMessage("file reading process", file);
reader.onloadend = function(e) {
postMessage("file reading process result", this.result);
};
reader.readAsText(file); // Read the file as plaintext.
}, onError);
}, onError);
}
//append data to file
function append(fs, filePath, blob) {
postMessage("inside append functuin fs ", fs, filePath, blob);
postMessage("inside append functuin filePath ", filePath);
postMessage("inside append functuin blob ", blob);
fs.root.getFile(filePath, {create: false}, function(fileEntry) {
// Create a FileWriter object for our FileEntry.
fileEntry.createWriter(function(fileWriter) {
fileWriter.seek(fileWriter.length); // Start write position at EOF.
fileWriter.write(bb.getBlob('text/plain'));
}, onError);
}, onError);
}
var bb;
function onFs(fs) {
postMessage("onFs function");
bb = new Blob();
postMessage("bb ", bb);
bb.append("helloo world" + '\n');
postMessage("bb after append", bb);
append(fs, 'log.txt', bb.getBlob('text/plain'));
postMessage("file writing", bb);
}
function onError(e) {
postMessage('ERROR: ' + e.toString());
}*