Search code examples
javascriptnode.jsjson2xls

Node json2xls downloadable file


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 as text and not as a downloadable file. Here is a screenshot at my console:

enter image description here

What am I doing wrong?

Here is a image of my request:

enter image description here


Solution

  • Have you tried telling express the filename manually like so

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

    I've had success with that before now.