How to download text content in a text area as a ANSI encoded text file instead of UTF-8 in JavaScript only

1.4k views Asked by At

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 ?

0

There are 0 answers