如何覆盖在Chrome应用程序中的文件? [英] How to overwrite a file in Chrome App?
问题描述
$ p $
chrome.fileSystem.chooseEntry({type:'openDirectory'},function(entry) {
chrome.fileSystem.getWritableEntry(entry,function(entry){
entry.getFile('file1.txt',{create:true},function(entry){
entry.createWriter (function(writer){
writer.write(new Blob(['Lorem'],{type:'text / plain'}));
});
});
entry.getFile('file2.txt',{create:true},function(entry){
entry.createWriter(function(writer){
writer.write(new Blob([ Ipsum'],{type:'text / plain'}));
});
});
});
});
覆盖一些现有文件 file1.txt
和 file2.txt
。但是我发现了一个问题:如果这些文件不是空的,它们的内容将不会被完全覆盖,只有开始的部分会被覆盖。
b
$ b
我需要先删除文件吗?或者我错过了什么?
看起来像 这代码为我工作,通过截断文件到写入完成后的位置。 I followed this example: to overwrite some existing file But I found a problem: if the files are not empty, their content won't be fully overwritten, only the beginning part will be overwritten. Do I need to remove the files first? Or do I miss something? It looks like This code worked for me, by truncating the file to the position of the writer after the write is finished.
这篇关于如何覆盖在Chrome应用程序中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! write
只覆盖在指定的职位< a>,所以你是正确的,如果你想完全替换文件的文本,你需要先删除文件或截断他们。
chrome.fileSystem.chooseEntry ({type:'openDirectory'},function(entry){
chrome.fileSystem.getWritableEntry(entry,function(entry){
entry.getFile('file1.txt',{create:true} ,function(entry){
entry.createWriter(function(writer){
writer.onwriteend = function(e){
e.currentTarget.truncate(e.currentTarget.position);
};
writer.write(new Blob(['Lorem'],{type:'text / plain'}));
});
});
entry.getFile('file2.txt',{create:true},function(entry){
entry.createWriter(function(writer){
writer.onwriteend = function(e) {
e.currentTarget.truncate(e.currentTarget.position);
};
write.write(new Blob(['Ipsum'],{type:'text / plain'} ));
});
});
});
});
chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {
chrome.fileSystem.getWritableEntry(entry, function(entry) {
entry.getFile('file1.txt', {create:true}, function(entry) {
entry.createWriter(function(writer) {
writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
});
});
entry.getFile('file2.txt', {create:true}, function(entry) {
entry.createWriter(function(writer) {
writer.write(new Blob(['Ipsum'], {type: 'text/plain'}));
});
});
});
});
file1.txt
and file2.txt
. write
only overwrites the contents of the file at the specified position, so you are correct that if you want to completely replace the text of the file, you'd need to either remove the files first or truncate them.chrome.fileSystem.chooseEntry({type:'openDirectory'}, function(entry) {
chrome.fileSystem.getWritableEntry(entry, function(entry) {
entry.getFile('file1.txt', {create:true}, function(entry) {
entry.createWriter(function(writer) {
writer.onwriteend = function(e) {
e.currentTarget.truncate(e.currentTarget.position);
};
writer.write(new Blob(['Lorem'], {type: 'text/plain'}));
});
});
entry.getFile('file2.txt', {create:true}, function(entry) {
entry.createWriter(function(writer) {
writer.onwriteend = function(e) {
e.currentTarget.truncate(e.currentTarget.position);
};
writer.write(new Blob(['Ipsum'], {type: 'text/plain'}));
});
});
});
});