Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File not downloading express.js res.download

I've generated/exported a xlsx file using json2xlsx npm module and to download that file I'm using res.download(file) functionality of of express.js.

Reference: Download a file from NodeJS Server using Express

Following is my code:

var fs = require("fs");
var json2xls = require('json2xls');

app.use(json2xls.middleware);

app.get('/export/:id', function (req, res) {
    var id = req.params.id;
    db.collection('provider').findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
        var jsonArr = {};
        var arr = jsonArr = doc;
        var xls = json2xls(arr);
        fs.writeFileSync('data.xlsx', xls, 'binary'); //file exported

        //Now I want to download that file
        res.setHeader('Content-disposition', 'attachment; filename=data.xlsx');
        res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        res.download(__dirname + '/public/data.xlsx', function (error) {
          console.log(error);
        });

        //res.json(doc);
    });
});

View html

        <div class="panel-body">

            <div class="form-group" ng-repeat="(key,value) in providerList"  ng-if="!$first">                    
                    <label>{{key.replace("_", " ") | uppercase}}</label>
                    <input type="text" class="form-control" id="title" placeholder="Enter Title" ng-model="providerList[key]">                    
            </div>

            <div class="well well-lg text-center bg-gray">                                        
                <button class="btn-lg btn-success" ng-click="export(id)">Export to Spreadsheet</button>
            </div>
        </div>

AngularJS controller code:

$scope.export = function (id) {
            $http.put('/export/' + id, $scope.providerList).success(function (response) {
                 if (response) {
                    alert("File is successfully exported at: "+ response);
                }
            });
        };

Error: { [Error: Request aborted] code: 'ECONNABORTED' }

Updated Error

enter image description here

Any help would be appreciated.

like image 538
J.K.A. Avatar asked Mar 14 '23 00:03

J.K.A.


2 Answers

Set response header before res.download(),

res.setHeader('Content-disposition', 'attachment; filename=data.xlsx');
res.setHeader('Content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.download(__dirname + '/data.xlsx');

UPDATE:

According to json2xls, you can use res.xls('data.xlsx', jsonArr);.

For that you have to setup middleware for json2xls

app.use(json2xls.middleware);

UPDATE 2:

For front end(AngularJS), Refer this

like image 121
Jay Avatar answered Mar 15 '23 13:03

Jay


May be your .xlsx file saved in the root folder.So, moved the .xlsx file under public folder and enable it in express like below.

        app.use(express.static('./public'));
like image 45
Vignesh Kumar Avatar answered Mar 15 '23 14:03

Vignesh Kumar