Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the HTML5 FileWriter truncate() method?
    primarykey
    data
    text
    <p>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).</p> <p>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 "").</p> <p>Here's a snippet of the FileWriter setup from HTML5Rocks, with truncate() added in.</p> <pre><code>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); </code></pre> <p>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.</p> <p>I've already tried looking through the <a href="http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-writing" rel="nofollow noreferrer">HTML5Rocks section</a> 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).</p> <p>Long story short, how I can I use the truncate() method correctly without getting an error?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload