//Создаем энкодер, который нам сделает ArrayBuffer в utf8
//Без параметра будет кодировка UTF8
const encoderUTF8 = new TextEncoder();
//Закодируем строку
const arrayUTF8 = encoderUTF8.encode("Привет!");
//Теперь создадим декодер в cp1251, список кодировок и их метки здесь
// https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
let encToCP1251 = new TextDecoder('cp1251');
//Получаем строку в нужной кодировке
const strCP1251 = encToCP1251.decode(arrayUTF8);
//Генерируем URL
const url = URL.createObjectURL(
//Создаем файл
new File([strCP1251], "foo.txt", {
type: "text/plain",})
);
//Создаём ссылку
let aElement = document.createElement('a');
aElement.href = url;
aElement.download = 'filenameCP1251.txt';
aElement.textContent = 'Скачай меня полностью!';
document.body.appendChild(aElement);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.