K
K
Kirill Kopitsa2018-11-27 14:05:21
JavaScript
Kirill Kopitsa, 2018-11-27 14:05:21

How to save .txt file in Windows-1251 with JS?

I use FileSaver.js to save the file, the result is a file with utf-8 encoding.
var blob = new Blob([text], { type: "text/plain; charset=windows-1251" });
saveAs(blob, 'file.txt');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Kopitsa, 2022-01-07
@Kir_K

In order to close the gestalt, I will add a solution that was found by trial and error. Maybe it will help someone to avoid wasting time searching like me.
So, the final code from that same project:

if (isIE) {
        var strResult = new TextEncoder('windows-1251', { NONSTANDARD_allowLegacyEncoding: true }).encode(txtData);
        safeSave('demirPayments.txt', strResult);
    } else {
        const encodedDataURL = windows1251.encodeURL(txtData);

        $('#LinkOnFile').attr('href', 'data:text/plain;charset=windows-1251,' + encodedDataURL);
        $('#LinkOnFile').find('span').trigger('click');
    };

Those. 2 different methods are used for IE and for other platforms. For IE, after converting the string to the encoding we need through TextEncoder, we use the following procedure to save the file, where we pack the file as binary data with the saveAs function from the FileSaver.js library:
function safeSave(name, Data, PathLength) {//Data as array             
    // !Elem:LoadSaveDi
    var safeSaveLoad = 0;
    if (!PathLength) { PathLength = 100000; }
    var buffer = new ArrayBuffer(Data.length)
        , temp = new DataView(buffer)
        , n32 = Math.floor(Data.length / 4)
        , n16 = Math.floor((Data.length - n32 * 4) / 2)
        , n8 = Data.length - n32 * 4 - n16 * 2
        , Kkey = 0, K = -1
        , APath = splitPath(n32, PathLength)
        ;
    if (APath == '') { APath = [0] }

    var RunSasa = setInterval(function () {
        if (Kkey == 0) { Kkey = 1; K += 1; }
        for (var i = 0; i < APath[K]; i++) {
            temp.setUint32((i + K * PathLength) * 4, Data[(i + K * PathLength) * 4] * Math.pow(256, 3) + Data[(i + K * PathLength) * 4 + 1] * Math.pow(256, 2) + Data[(i + K * PathLength) * 4 + 2] * 256 + Data[(i + K * PathLength) * 4 + 3]);
        }
        Kkey = 0;
        safeSaveLoad = Math.round(1000 * (K + 1) / APath.length) / 1000;
        if (K + 1 == APath.length) {
            clearInterval(RunSasa);
            if (n16 != 0) { temp.setUint16(n32 * 4, Data[n32 * 4] * 256 + Data[n32 * 4 + 1]); }
            if (n8 != 0) { temp.setUint8(n32 * 4 + n16 * 2, Data[n32 * 4 + n16 * 2]); }
            saveAs(new Blob([buffer], { type: "binary" }), name);
        }
    }, 10);
}

Other browsers refused to work according to this scheme, as well as other more or less trivial ones (I will not give a list of tried ones, because it was a long time ago and it is possible that now some of them will work on new versions of browser engines).
However, then I had to simulate a link and a click on it:
$('#LinkOnFile').attr('href', 'data:text/plain;charset=windows-1251,' + encodedDataURL);
$('#LinkOnFile').find('span').trigger('click');

This is the only way to get the desired file format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question