简体   繁体   English

Nodejs:如何在 windows-1252 中发送 http 响应

[英]Nodejs: how to send the http response in windows-1252

I have a nodejs site and at some point it can generate a XML file fomr a database.我有一个 nodejs 站点,在某些时候它可以生成一个数据库的 XML 文件。 The problem is that all is in utf-8 but for this specific case, i need that both content and file are in windows-1252.问题是所有内容都在 utf-8 但对于这种特定情况,我需要内容和文件都在 windows-1252 中。

                const W1252 = require('windows-1252'); //https://www.npmjs.com/package/windows-1252
                ...
                r.header('Content-Disposition', 'attachment; filename=sample.xml');
                r.header('Content-Type', 'text/xml; charset=windows-1252');
                r.write(W1252.encode(`<?xml version="1.0" encoding="windows-1252"?><x>€Àáção</x>`, {
                    'mode': 'html'
                }));
                r.end();
                ...

But this does not work.但这不起作用。 When the client browser downloads this file, it saves it as charset utf-8.当客户端浏览器下载此文件时,它会将其保存为字符集 utf-8。 I test it doing:我测试它做:

file -i sample.xml 
text/xml; charset=utf-8

The browser response headers mentions windows-1252.浏览器响应标头提到 windows-1252。 I am puzzled?我很疑惑? Can anyone tell me what i am missing.谁能告诉我我错过了什么。 Thank you.谢谢你。 浏览器响应标头提到 windows-1252

I will use the iconvlite:我将使用 iconvlite:

global.ICONVLITE = require('iconv-lite');

            res.header('Content-Disposition', 'attachment; filename=sample.xml');
            res.header('Content-Type', 'text/xml; charset=iso8859-1');
            res.write(ICONVLITE.encode(`<?xml version="1.0" encoding="windows-1252"?><x>Àáção</x>`, "iso8859-1"));
            r.end();

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

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