Jordan.J.D
Jordan.J.D

Reputation: 8113

large files failing download in nodejs

I have a service that builds a csv file, and returns it to the user. Currently using expressjs v4.14, nodejs v8.7.0. My problem is that I get a download failed due to 'network error' in chrome when I call the service for it to create a large csv file. With smaller files, the service works fine. I can also browse to the /temp/ folder, and the entire expected file exists. In each case of 'things i tried', I was able to download smaller files but not the large ones.

Service:

download.post('/csv', (req, res, next) => {

        res.status(200).header('Content-Type', 'text/csv');
        const newUUID = uuid.v1();

        let ws: WriteStream = fs.createWriteStream(`${__dirname}/../../temp/${newUUID}.csv`);

        ws.on('finish', () => {
            res.download(`${__dirname}/../../temp/${newUUID}.csv`);
        });

        //csv file built here 
        ws.write('huge stuff easily 50k rows and 10 mb file');
        ws.end(); 
});

Chrome Error:

Chrome's network tab and developer console do not give me any indication of what happened. This download popup is all I get. I cleared everything in cookies/cache just in case and it did not help.

enter image description here

Things I tried:

Update:

Ended up trying the service from postman and it worked, so I think it is an angularjs issue.

Angularjs

    $http({
            cache: false,
            url: "/download/csv",
            headers: {
                'accept': 'text/csv'
            },
            method: 'POST',
            data: {
                rows: rows,
                title: title ? title : ''
            }
        }).success(function (data, status, headers, config) {
        var anchor = angular.element('<a/>');
        anchor.attr({
            href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
            target: '_blank',
            download: 'csv_info.csv'
        })[0].click();
    }).error(function (data, status, headers, config) {

    });

Upvotes: 2

Views: 1307

Answers (1)

Jordan.J.D
Jordan.J.D

Reputation: 8113

Turned out to be a limitation from href attribute from the anchor tag created in angularjs. This was solved using FileSaver:

$http({
        cache: false,
        url: "/download/csv",
        headers: {
            'accept': 'text/csv'
        },
        method: 'POST',
        data: {
            rows: rows,
            title: title ? title : ''
        }
    }).success(function (data, status, headers, config) {
           var file = new File([data], "info.csv", {type: "text/csv;charset=utf-8"});
           saveAs(file);
}).error(function (data, status, headers, config) {

});

Upvotes: 1

Related Questions