0

I want to download text content in a text area as ANSI encode text file instead of UTF-8.

As the answer in here I would able to download a text file. But the problem is encoding in UTF-8

I tried like below by changing type but not worked

function saveTextAsFile(textToWrite, fileNameToSaveAs) {
    var textFileAsBlob = new Blob([textToWrite], {
        type: 'text/plain;charset=ANSI'
    });
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null) {
        // Chrome allows the link to be clicked
        // without actually adding it to the DOM.
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    } else {
        // Firefox requires the link to be added to the DOM
        // before it can be clicked.
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }

    downloadLink.click();
}

Are there any method to do this ?

Chamod
  • 587
  • 2
  • 6
  • 17
  • The [TextEncoder API](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) no longer supports non-UTF-8 encoding and recommends using the [inexorabletash/text-encoding](https://github.com/inexorabletash/text-encoding) polyfill package. Starting with that package may help. This question has some examples of how the package can be used: https://stackoverflow.com/questions/59840163/change-javascript-blob-encoding-to-ansi-instead-of-utf-8 – Chase Ingebritson Oct 05 '20 at 18:49
  • Actually https://stackoverflow.com/questions/56092285/blob-charset-for-csv-file/56125587#56125587 would be a better dupe target (had it an upvoted answer). The one linked by @ChaseIngebritson was a particular case where OP needed the `endings` option of `Blob` contructor. – Kaiido Oct 19 '20 at 07:24

0 Answers0