I have a typical JPG file that I download using axios and store on a disk like so:
// Just create axios instance that does not care about HTTPS certificate validity
// since I need to download from an untrusted internal service.
// Included for completeness. Do not borrow this bit unless you know what you are doing.
const axiosInstance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
// Download file
fileData = await axiosInstance.get(imageUrl, {
responseType: 'arraybuffer',
});
console.log(`Dowloaded OK, size ${fileData.data.length} bytes`));
// Dump file to disk
await fs.writeFile(`${filename}`, fileData.data, (err) => {}
});
I observe that while console.log reports correct number of bytes and fileData.data looks OK in debugger, the file recorded has twice as many bytes and naturally is corrupted. Seems that non-English letter became 2 bytes instead of one. I suspect it has something to do with the encoding.
How I could possibly correct this so the file is recorded correctly?