简体   繁体   English

NodeJs - 如何使用BOM编写函数fs.writeFile?

[英]NodeJs - how to make function fs.writeFile write with BOM?

I'm using nodeJS v0.8.6 and the native library fs. 我正在使用nodeJS v0.8.6和本机库fs。 Here is my code : 这是我的代码:

var filesys = require('fs');
filesys.writeFile('test.txt', 'This is an example with accents : é è à ','utf8', function (err) {});

The problem is that it writes in utf8 without BOM (I use notepad++ to verify it) and it doesn't work in wordpad on Windows (the accents are not well displayed). 问题是它在没有BOM的情况下写入utf8(我使用notepad ++来验证它),并且它在Windows上的wordpad中不起作用(重音没有很好地显示)。 The thing is that I need that file to be well read by womeone using wordpad. 问题是我需要womeone使用wordpad很好地阅读该文件。

How can I add the BOM to my file ? 如何将BOM添加到我的文件中?

UTF-8不需要bom,但你可以自己添加它。

filesys.writeFile('test.txt', '\ufeffThis is an example with accents : é è à ','utf8', function (err) {});

I elaborated on this answer in detail on this answer - Adding UTF-8 BOM to string/Blob . 我在这个答案中详细阐述了这个答案 - 将UTF-8 BOM添加到string / Blob

This is a very sparse answer that doesn't go into detail as to why this works. 这是一个非常稀疏的答案,没有详细说明为什么这样做。 The FEFF bytes are actually the UTF16LE BOM, so the previous answer is confusing. FEFF字节实际上是UTF16LE BOM,因此之前的答案令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM