I need to download the contents of a table shown in the view as a CSV file. I have code to format the contents of the table properly so that it can be stored as a .csv file (for example, the column separator is a comma, each record of the table is in a new line).
So, in my controller, I have the following code:
window.location.href = '/download?data=' + encodeURIComponent($scope.csvString);
I am using NodeJs for the server part. Here, I have the following route handler:
app.get('/download', function (req, res) {
var data = req.query.data;
res.attachment('data.csv');
res.setHeader('Content-Type', 'text/csv');
res.end(data);
});
This works perfectly fine. The download works and the data in the file downloaded is in perfect CSV format. No issues.
The issue arises when there are too many records in the table, that is when the data is huge. Due to this, the query parameter is really huge and I assume that it exceeds the limit and thus, the GET request returns with a 400 (Bad Request) error - note that according to my analysis, the request never reaches the server, instead I observed that the request failed using the Chrome Developer Tools. In the browser, the URL simply is www.my_domain.com/download?data=the_lengthy_data
. That's it. The web page is blank / empty and no file is downloaded.
Thus, I wish to know how I can download the lengthy data? All examples that I came across do a GET request and thus I have to pass the lengthy data as a query parameter. Is there some other approach?