Search code examples
node.jsxmlhttpwindows-1252

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. 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.

                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. I test it doing:

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

The browser response headers mentions windows-1252. I am puzzled! Can anyone tell me what i am missing? Thank you. The browser response headers mention windows-1252


Solution

  • I will use the 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();