Petschko
Petschko

Reputation: 178

JavaScript CSV-Text download in ANSI (Windows-1252)

I try to create a CSV-File download with Javascript.

We need to export Data from our Website to a 3rd party Program, the creation and download works pretty well. There is just one Problem, I need the CSV-File encoded in ANSI (Windows-1252) - the 3rd Party Program is very old and can't understand Multi-Byte Encodings. My Files are served in UTF-8 and I found no way until now to convert the File to ANSI, I get always UTF-8-Content...

The current hotfix is to open the file and manually change the encoding to ANSI, but thats not nice and for some in my Company difficult to do (sometimes they forget it, because they are not really comfortable with PCs).

I want a File that's ready to use in ANSI, not in UTF-8. I can convert the File in PHP, it has the correct encoding and content, but I have no write Access on the Server, thats why I need to download the File dynamic with AJAX.

I used a solution like him on Stackoverflow: https://stackoverflow.com/a/22089405/5092608

But I get UTF-8 as Content.

I went a step back and try to convert with JavaScript on a very simple page, but I got UTF-8 as well...:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
        <title>My File-Download</title>
        <script type='text/javascript' src='encoding.js'></script>
        <script type='text/javascript' src='encoding-indexes.js'></script>
        <script type='text/javascript' src='FileSaver.min.js'></script>
        <script type='text/javascript'>
        <!--

            /**
             * Downloads the Text as File
             *
             * @param {string} filename - Name of the File
             * @param {string} text - Content of the File
             */
            function downloadExportFile(filename, text) {
                var encodedText = new TextEncoder("windows-1252", {NONSTANDARD_allowLegacyEncoding: true}).encode([text]);
                var blob = new Blob([encodedText], {type: 'text/plain;charset=windows-1252;'});
                saveAs(blob, filename);
            }

        // -->
        </script>
    </head>
    <body>
        <a href="#" onclick="downloadExportFile('export.csv', 'Dragicevic,Peter,,1,Straße 4,21027,Hamburg,43,,,,,,,,');">Download Windows-1252 CSV-File</a>
    </body>
</html>

I didn't found much about convert UTF-8 to ANSI (but in the other direction are tons of solutions). Did anybody know a solution to get a ANSI (Windows-1252)-File with JavaScript?

Upvotes: 6

Views: 8567

Answers (1)

Paulo Henrique Arruda
Paulo Henrique Arruda

Reputation: 489

I hope it helps!

Polyfill for the Encoding Living Standard's API

Download both files encoding-indexes.js and encoding.js

HTML sample:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />

    <title></title>

    <script>
        window.TextEncoder = window.TextDecoder = null;
    </script>

    <script type="text/javascript" src="encoding-indexes.js"></script>    
    <script type="text/javascript" src="encoding.js"></script>

    <script>
        function BlobDownload(Filename, Bytes, Mimetype) {
            var filData = new Blob(Bytes, { type: Mimetype });
            if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
                window.navigator.msSaveOrOpenBlob(filData, Filename);
            } else { // for Non-IE (chrome, firefox etc.)
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.style = "display: none";
                var filUrl = URL.createObjectURL(filData);
                a.href = filUrl;
                a.download = Filename;
                a.click();
                a.remove();
            }
        };

        function download() {
            var bytes = new TextEncoder("windows-1252", { NONSTANDARD_allowLegacyEncoding: true }).encode("Eu não tenho mais dúvidas");

            BlobDownload("test_windows1252_encoding.txt", [bytes]);
            BlobDownload("test_windows1252_encoding.csv", [bytes], "text/csv");
        }
    </script>
  </head>
  <body>
    <button onclick="download()">Download</button>
  </body>
</html>

Upvotes: 3

Related Questions