【发布时间】:2021-05-14 12:25:46
【问题描述】:
使用nodejs和iconv-lite在xml中创建一个字符集windows-1252的http响应文件,file -i命令无法识别为windows-1252。
服务器端:
r.header('Content-Disposition', 'attachment; filename=teste.xml');
r.header('Content-Type', 'text/xml; charset=iso8859-1');
r.write(ICONVLITE.encode(`<?xml version="1.0" encoding="windows-1252"?><x>€Àáção</x>`, "win1252")); //euro symbol and portuguese accentuated vogals
r.end();
浏览器下载文件,然后我在 Ubuntu 20.04 LTS 中检查它:
file -i teste.xml
/tmp/teste.xml: text/xml; charset=unknown-8bit
当我使用 gedit 打开它时,重读的 vogal 看起来很好,但欧元符号却没有(从 128 到 159 的所有字符都搞砸了)。
我签入了一个 Windows 10 虚拟机,一切顺利。在 Windows 和 Linux 网络浏览器中,它也显示一切正常。
那么,文件命令有问题吗?如何在 Linux 中检查文件的正确字符?
谢谢
编辑 结果文件可以得到here
第二次编辑 我发现一个错误!代码行:
r.header('Content-Type', 'text/xml; charset=iso8859-1');
必须是:
r.header('Content-Type', 'text/xml; charset=Windows-1252');
【问题讨论】:
-
您能否将
od teste.xml的结果粘贴到您的问题中? -
是的,对不起...完成了。
-
我用正确的文件替换了下载文件。我还发现,现在这个文件给出了 charset unknown-8bit 但如果我在上面添加更多字符,它会返回 iso-8859-1。因此,必须通过某些字符触发 file -i 的结果发生变化而不改变 content-type 和 iconv 编码。
标签: node.js linux file windows-1252 cp1252