A
A
Alexander Wolf2015-10-19 20:54:10
JavaScript
Alexander Wolf, 2015-10-19 20:54:10

How to defeat encodings under NodeJS?

Good evening! I generate a file on the server with a Russian-language name and encoding windows-1251. This is necessary for the correct processing of the csv file by excel.

var name = iconv.encode(poll.name, 'win1251');
    res.setHeader('Content-Disposition', `attachment; filename="${name}.csv"`);
    res.setHeader('Content-Type', 'text/csv; charset=windows-1251');
    var str = "sep=,\r\n\"" + poll.name + "\"\r\n\r\n" + json2csv(answers, true, true);
    res.end(iconv.encode(str, 'win1251'));

But when downloading, I get the file "eeeeee.csv". How to overcome this miracle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2015-10-20
@mannaro

The file name encoding in HTTP headers is independent of the file content encoding. In general, this is a separate minefield with a million bugs. Check out greenbytes.de/tech/tc2231 for a lot of interesting stuff.
In general, for more or less modern browsers, you should choose the option with encoding in UTF-8 greenbytes.de/tech/tc2231/#attwithfn2231utf8. To be safe, duplicate the filename in the download HTML attribute of the download link. It has no encoding problems and is supported by Firefox, Chrome (and everything on it), support in Edge is planned. In short, he won't make it worse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question