2

I have the following (simple) node module:

var json2xls = require('json2xls');
var fs = require('fs');
module.exports = function (router, mongoose) {
    router.route('/client/toExcel')
        .post(function (req, res) {
            var obj = req.body.data;
            var xls = json2xls(obj);

            fs.writeFileSync('data.xlsx', xls, 'binary');
            res.download('data.xlsx');

        });
    return router;
};

Now when i call this from my frontend. The node server creates the file however it sends back the file but as text and not as a downloadable file here is a screenshot at my console:

enter image description here

Can anyone tell me what im doing wrong?

Here is a image of my request:

enter image description here

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

2 Answers2

2

Have you tried telling express the filename manually like so

res.download('/data.xlsx', 'data.xlsx');

I've had success with that before now.

Glitcher
  • 1,078
  • 6
  • 14
  • How do I download in client-side(browser) without writing the file in the server? – Sanjay Shr Nov 26 '18 at 14:29
  • It's not working. I am getting the below error in excel. Removed Part: /xl/sharedStrings.xml part with XML error. (Strings) Illegal xml character. Line 1, column 153. Removed Records: Cell information from /xl/worksheets/sheet.xml part – Rohit Vyas Jun 02 '21 at 15:48
  • that's a problem with your data. – Glitcher Jun 03 '21 at 11:26
0

You can directly download your file by just calling your API

var json2xls = require('json2xls');
var fs = require('fs');
module.exports = function (router, mongenter code hereoose) {
    router.route('/client/toExcel')
        .post(function (req, res) {
            var obj = req.body.data;

            res.xls('data.xlsx', obj);
        });
    return router;
};