最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using the HTML5 FileWriter truncate() method? - Stack Overflow

programmeradmin8浏览0评论

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 talking about the FileWriter truncate() method, and I know it does what I need to do. Basically, rather than appending text to some file data or using seek() to overwrite a certain portion, I want to overwrite all of the data with something else (e.g. from "somedata" to "").

Here's a snippet of the FileWriter setup from HTML5Rocks, with truncate() added in.

function onInitFs(fs) {

  fs.root.getFile('log.txt', {create: false}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.seek(fileWriter.length); // Start write position at EOF.
      fileWriter.truncate(1);

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

    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);

When it gets to calling writer.truncate(), calling writer.write() throws a File Exception error. I believe this is because the readyState is set to WRITING. Unfortunately, I don't know how to get around that.

I've already tried looking through the HTML5Rocks section on this, but it doesn't tell me anything about a truncate() method (although I know it exists from what the Webkit JS Console tells me).

Long story short, how I can I use the truncate() method correctly without getting an error?

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 talking about the FileWriter truncate() method, and I know it does what I need to do. Basically, rather than appending text to some file data or using seek() to overwrite a certain portion, I want to overwrite all of the data with something else (e.g. from "somedata" to "").

Here's a snippet of the FileWriter setup from HTML5Rocks, with truncate() added in.

function onInitFs(fs) {

  fs.root.getFile('log.txt', {create: false}, function(fileEntry) {

    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function(fileWriter) {

      fileWriter.seek(fileWriter.length); // Start write position at EOF.
      fileWriter.truncate(1);

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

    }, errorHandler);

  }, errorHandler);

}

window.requestFileSystem(window.PERSISTENT, 1024*1024, onInitFs, errorHandler);

When it gets to calling writer.truncate(), calling writer.write() throws a File Exception error. I believe this is because the readyState is set to WRITING. Unfortunately, I don't know how to get around that.

I've already tried looking through the HTML5Rocks section on this, but it doesn't tell me anything about a truncate() method (although I know it exists from what the Webkit JS Console tells me).

Long story short, how I can I use the truncate() method correctly without getting an error?

Share Improve this question edited May 13, 2017 at 21:41 caleb531 asked Jul 22, 2011 at 15:50 caleb531caleb531 4,3616 gold badges33 silver badges42 bronze badges 2
  • 2 W3C truncate spec enjoy. – Raynos Commented Jul 22, 2011 at 15:55
  • 1 Why truncate(1)? Why not truncate(0) to get a zero-sized file? – huyz Commented Aug 23, 2011 at 5:18
Add a ment  | 

2 Answers 2

Reset to default 11

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 pleted.');
    };
    fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
    };
    var blob = new Blob(['helo'], {type: 'plain/text'});
    fileWriter.write(blob);
}, errorHandler);

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)

  1. 暂无评论