I want to create and download text file in windows-1251
format. When I'm trying to use the code below, the file is downloading successfully, but, unfortunately, in utf-8
format. Any suggestions on what am I doing wrong?
nodeCode:
//node-version: v12.13.0
var iconv = require("iconv-lite");
//create txt file
router.post("/to-text", (req, res) => {
const arr = req.body.arr; //an array of objects
var file2 = iconv
.encodeStream("win1251")
.pipe(fs.createWriteStream(`filepath`));
file2.on("error", function (err) {
return res.status(500).json(err);
});
arr.forEach(function (v) {
const lastChar = v[1].slice(-1);
if (lastChar === "я") {
v[1] = v[1] + ".";
}
file2.write(v.join(",") + "\n");
});
file2.end();
returnres.send(file2);
});
//dowload txt file
router.get("/download", (req, res) => {
let file_path = `filepath`;
res.download(file_path);
});
ReactCode:
const getText = () => {
let arr = [];
const date = `PLU_${Moment().format("L").split(".").join("")}.txt`;
productsList.map((e, idx) =>
arr.push(
Object.values({
a: e.hotkey,
b: e.name,
c: "",
})
)
);
// first request to save as text in filepath
Axios({
method: "POST",
url: "/myapi/to-text",
data: {
arr,
date,
type: prefixType.value,
},
responseType: "blob",
})
.then((data) => {
return data.data;
})
.then((resp) => {
closeModal();
return Axios.get("myapi/download", {
responseType: "blob",
params: { date },
})
.then((res) => res.data)
.then((response) => {
const url = window.URL.createObjectURL(
new Blob(
[
response,
],
{
type: "text/plain;charset=windows-1251",
}
)
);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", date);
document.body.appendChild(link);
link.click();
});
})
.catch((err) => {
ErrorAlert(err);
});
};
When in React the function fires, it first creates a txt
file in the file path on the server-side. Then downloads it, using a second request.
To check the file formats I'm using Notepad++
, just by opening it in the editor and viewing the below-right corner.
I guess that some small detail is missing, to convert the file properly, but I can not see it( Thanks in advance.
I've found the solution to my problem. I was needed to encode it only once, inside the loop, while .writing
it into the file, like:
file.write(iconv.encode(v.join(",") + "\n", "win1251"));
That's it, after this little manipulation it saves the file as windows-1251
. Obviously, downloads in the same format.