fs.writefile used from node-webkit only writes part of the buffer

2 views
Skip to first unread message

Rick H.

unread,
Apr 13, 2014, 6:57:32 PM4/13/14
to node-...@googlegroups.com
Hello, all.  I'm not sure whether this behavior of node's is only in a node-webkit context, or not.  

I have the following code: 

            function saveFileAs() {
                var inputSaveAsFile = document.querySelector(inputSaveAsFileId);
                inputSaveAsFile.addEventListener('change', function(evt) {
                    var textBox = document.querySelector('#theText');
                    var data = textBox.innerHTML;
                    var path = this.value;
                    fs.writeFile(path, data, {encoding: 'ascii'}, function(err) {
                        if (err) throw err;
                        console.log('writeFile succeeded.');
                    });
                }, false);
                inputSaveAsFile.click();
            }

I can type something into my contenteditable div that has the id "theText," and use my file menu to come to the saveFileAs function.  It opens the file dialog as expected, and I come to the fs.writeFile call.  (I have an input with type file and the attribute nwsaveas.)  Right before the call, the parameters have these values:

path
"C:\node-webkit\experiment\Inigo_Sunday.txt"
data
"Hallo.<div>My name is Inigo Montoya.</div><div>You killed my father.</div><div>Prepare to die.</div>"

After the call I type out the file, and it isn't all the data: 

Rick@RicksLaptop /cygdrive/c/node-webkit/experiment
$ cat Inigo_Sunday.txt
Hallo.<div>My name is Inigo Montoya.</div><div>You
Rick@RicksLaptop /cygdrive/c/node-webkit/experiment

I am mystified.  Do you understand it? 

             Regards, Rick

Rick H.

unread,
Apr 13, 2014, 7:05:19 PM4/13/14
to node-...@googlegroups.com
Hey!  I just realized.  The information that appears in the file is exactly 50 characters: 

Rick@RicksLaptop /cygdrive/c/node-webkit/experiment
$ od -bc Inigo_Sunday.txt
0000000 110 000 141 000 154 000 154 000 157 000 056 000 074 000 144 000
          H  \0   a  \0   l  \0   l  \0   o  \0   .  \0   <  \0   d  \0
0000020 151 000 166 000 076 000 115 000 171 000 040 000 156 000 141 000
          i  \0   v  \0   >  \0   M  \0   y  \0      \0   n  \0   a  \0
0000040 155 000 145 000 040 000 151 000 163 000 040 000 111 000 156 000
          m  \0   e  \0      \0   i  \0   s  \0      \0   I  \0   n  \0
0000060 151 000 147 000 157 000 040 000 115 000 157 000 156 000 164 000
          i  \0   g  \0   o  \0      \0   M  \0   o  \0   n  \0   t  \0
0000100 157 000 171 000 141 000 056 000 074 000 057 000 144 000 151 000
          o  \0   y  \0   a  \0   .  \0   <  \0   /  \0   d  \0   i  \0
0000120 166 000 076 000 074 000 144 000 151 000 166 000 076 000 131 000
          v  \0   >  \0   <  \0   d  \0   i  \0   v  \0   >  \0   Y  \0
0000140 157 000 165 000
          o  \0   u  \0
0000144

Rick@RicksLaptop /cygdrive/c/node-webkit/experiment

That's the length of the original contents of the contenteditable div.  Here's how the div is set up: 

        <div id="theText" contenteditable="true">
            Please enter your text here.
        </div>

With the CR and the 12 spaces before the text, and the CR and the 8 spaces after the text, the original contents of the div is exactly 50 characters. 

That's got to mean something.  But what? 

Roger

unread,
May 8, 2014, 9:49:59 PM5/8/14
to node-...@googlegroups.com, cub...@gmail.com
I just found that the string you read from input widget is encoded
with utf16le internally in 0.9.x. So if you convert it to Buffer or
write it you should use that encoding.

See https://github.com/rogerwang/node-webkit/issues/1669
> --
> You received this message because you are subscribed to the Google Groups
> "node-webkit" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-webkit...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages