Answer the question
In order to leave comments, you need to log in
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
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');
};
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);
}
$('#LinkOnFile').attr('href', 'data:text/plain;charset=windows-1251,' + encodedDataURL);
$('#LinkOnFile').find('span').trigger('click');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question