使用 HTML5 FileWriter truncate() 方法?

发布于 2024-11-25 12:53:46 字数 1287 浏览 2 评论 0原文

我正在尝试 HTML5 File API,我需要使用一种我不太了解的方法(只是因为它几乎没有在任何地方记录)。

我正在谈论 FileWriter truncate() 方法,我知道它可以完成我需要做的事情。基本上,我不想将文本附加到某些文件数据或使用seek() 覆盖特定部分,而是想用其他内容覆盖所有数据(例如从“somedata”到“”)。

下面是 HTML5Rocks 中 FileWriter 设置的片段,其中添加了 truncate()。

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

当调用 writer.truncate() 时,调用 writer.write() 会引发文件异常错误。我相信这是因为readyState被设置为WRITING。不幸的是,我不知道如何解决这个问题。

我已经尝试过浏览 HTML5Rocks 部分 对此,但它没有告诉我有关 truncate() 方法的任何信息(尽管我从 Webkit JS 控制台告诉我的内容知道它存在)。

长话短说,如何正确使用 truncate() 方法而不出现错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-12-02 12:53:46

像这样的事情可能更切题:

截断将文件的长度更改为指定的长度

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

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);
情场扛把子 2024-12-02 12:53:46

你需要更加异步。 :)

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文