6

Ho seguito questo esempio:Come sovrascrivere un file nell'app Chrome?

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'})); 
      }); 
     }); 
    }); 
}); 

di sovrascrivere alcuni file esistente file1.txt e file2.txt.

Ma ho trovato un problema: se i file non sono vuoti, il loro contenuto non verrà completamente sovrascritto, solo la parte iniziale verrà sovrascritta.

Devo prima rimuovere i file? O mi manca qualcosa?

risposta

3

Sembra write sovrascrive solo il contenuto del file alla specificato position, così si è corretto che, se si desidera sostituire completamente il testo del file, avresti bisogno di rimuovere i file prima o troncare .

Questo codice ha funzionato per me, troncando il file nella posizione dello scrittore al termine della scrittura.

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'})); 
      }); 
     }); 
    }); 
}); 
+4

Per me la funzione troncata ha attivato l'evento onwriteend, risultando in un ciclo infinito. Tuttavia [questa soluzione] (http://stackoverflow.com/questions/19426698/overwrite-a-file-with-html5-filewriter) ha funzionato per me. – user495285

Problemi correlati