Nodejs Express wants to send Buffer with BOM in Response

Asked 2 years ago, Updated 2 years ago, 42 views

Download the csv via the API you are creating in Express and download the file with UTF-8 with BOM for Excel use.

res.contentType('text/csv');
res.attachment('response.csv');
res.write(
  Buffer.concat ([Buffer.from([0xef, 0xbb, 0xbf], Buffer.from('csv, string, here')])
);
res.end();

As a result, the csv file with UTF-8 without BOM is downloaded.
I have verified that the res.write contents of fs.write are written to the file as UTF-8 with BOM, so the BOM seems to be missing during the HTTP response process.
If you know any countermeasures, please let me know.

node.js

2022-09-29 22:53

1 Answers

This may be a client-side issue to retrieve.

I accessed the browser using the following code. The BOM was left in the files downloaded by Chrome in OS X and IE11 in Win.
node —v10.6.0(OSX)
express:4.17.1

I corrected the questionnaire, but there was an unnecessary semicolon at the end of the Buffer line.If it still exists in the actual code, it may be a different problem, so I will let you know just in case.

var express=require('express')
var app=express()

app.get('/', function(req,res){
    res.contentType('text/csv');
    res.attachment('response.csv');
    res.write(
        Buffer.concat ([Buffer.from([0xef, 0xbb, 0xbf], Buffer.from('csv, string, here')])
    );
    res.end();
})
app.listen(3000,()=>console.log('3000'))

The image is an IE downloaded file opened in a binary editor.

Enter a description of the image here


2022-09-29 22:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.