Using the HTML5 FileWriter truncate() method?

前端 未结 2 508
灰色年华
灰色年华 2021-02-14 10:50

I\'m experimenting with the HTML5 File API, and I\'m needing to use a method which I don\'t know enough about (simply because it\'s hardly documented anywhere).

I\'m tal

相关标签:
2条回答
  • 2021-02-14 11:02

    You need to be more async'y. :)

    fileEntry.createWriter(function(fileWriter) {
    
      fileWriter.onwriteend = function(trunc) {
        fileWriter.onwriteend = null; // Avoid an infinite loop.
        // Create a new Blob and write it to log.txt.
        var bb = new BlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12.
        bb.append('Hello World');
        fileWriter.write(bb.getBlob('text/plain'));
      }
    
      fileWriter.seek(fileWriter.length); // Start write position at EOF.
      fileWriter.truncate(1);
    
    }, errorHandler);
    
    0 讨论(0)
  • 2021-02-14 11:23

    Something like this might be a little more to the point:

    truncate Changes the length of the file to that specified

    fileEntry.createWriter(function(fileWriter) {
        var truncated = false;
        fileWriter.onwriteend = function(e) {
            if (!truncated) {
                truncated = true;
                this.truncate(this.position);
                return;
            }
            console.log('Write completed.');
        };
        fileWriter.onerror = function(e) {
            console.log('Write failed: ' + e.toString());
        };
        var blob = new Blob(['helo'], {type: 'plain/text'});
        fileWriter.write(blob);
    }, errorHandler);
    
    0 讨论(0)
提交回复
热议问题