Writing text files with node webkit

ⅰ亾dé卋堺 提交于 2019-12-25 01:54:11

问题


I'm trying to do something rather simple: write a text file with data entered in a text input field to a file...

var data = document.getElementById("fileContent").value;
fs.writeFileSync("test.txt", data);

For instance if I type in,

Write this to file 123 123

I end up with this in the file...

Write this to

If I hard code a string into the application, it writes correctly.

fs.writeFileSync("test.txt", "this is a hard coded string");

I tried using writeFileSync with and without the encoding parameter set. I've tried createWriteStream with and without encoding the parameter set. I've tried fileOpen, fs.writeSync, and fs.close. I even tried converting the date to a Buffer object and writing that. In every case, I got the exact same results.

The encoding is also strange. Notepad++ indicates that the encoding is "UCS2-LE w/o BOM" I'd expect it to be UTF-8, as I'v been setting the encoding parameter to that.

Any thoughts?


回答1:


  1. It's a bug with Node-Webkit-v0.9.*
  2. It's OK if you use Node-Webkit-v.8.* or lower version.



回答2:


After some more research and determining it was something with encoding, I stumbled on this post. Apparently, utf8 doesn't work...

https://groups.google.com/forum/#!msg/node-webkit/3M-0v92o9Zs/eSYnSZ8dUK0J

I changed the encoding it to "utf16le", and this appears to write the text correctly for hard-coded text and text from a text box.



来源:https://stackoverflow.com/questions/23290362/writing-text-files-with-node-webkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!