Hi,
I need to generate and download pdf file on server in node js. I have created api for that. When i directly call api in browser, it works fine and download file but when I call file from axios post, it of my client side code, it is not downloding file. Following is my code. Please suggest.
- app.get("/getpdf/*", function (req, res) {
- var query = "SELECT UserId,UserName,Password,UserType,Mobile,Status,Password1,Password2,Password3,replace(CONVERT(VARCHAR(10), DateCreated, 103),'01/01/1900','-') FROM AppUsers";
- var fields = "User Id,Username,Password,Usertype,Mobile,Status,Password1,Password2,Password2,Created Date";
- //GetPDF(res, query, fields);
- GetCSV(res, query);
- });
- function GetPDF(res, qry, fields) {
- var request = new sql.Request();
- // query to the database and get the records
- request.query(qry, function (err, result) {
- if (err) {
- console.log(err);
- res.send(err);
- }
- else {
- let rdata = result.recordsets[0];
- var ShowLabel = true;
- var JSONData = result.recordsets[0];
- var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
- var number_of_rows = rdata.length;
- var table_body = '';
- table_body += '';
- table_body += '
;Appuser List
' - table_body += '';';
- table_body += '
';
- var str_Fields = fields.split(',');
- table_body += '
'; - for (i = 0; i < str_Fields.length; i++) {
- if (i % 6 == 0)
- {
- table_body += '
'; ';- }
- table_body += '
' + str_Fields[i] + ' ';- }
- table_body += '
- for (var i = 0; i < arrData.length; i++) {
- var cssclass = '';
- if (i % 2 == 0) {
- cssclass = 'r1';
- }
- else {
- cssclass = 'r2';
- }
- table_body += '
+ cssclass + '">'; - var k = 0;
- for (var index in arrData[i]) {
- if (k % 6 == 0) {
- table_body += '
+ cssclass + '">'; ';- }
- table_body += '
'; ';- table_body += arrData[i][index];
- table_body += '
- k++;
- }
- table_body += '
- }
- table_body += '
- table_body += '
- var d = new Date();
- var fname = d.getDate() + "" + d.getMonth() + "" + d.getYear() + "" + d.getHours() + "" + d.getMinutes() + "" + d.getSeconds();
- res.header('Content-Disposition', 'attachment; filename=' + fname + '.pdf');
- pdf.create(table_body, options).toStream(function (err, stream) {
- stream.pipe(res);
- });
- }
- });
- }
Client side code:
- ExportPdf()
- {
- axios.get('http://127.0.0.1:9000/getpdf/*', {data: this.data}).then(res => {
- });
- },
Replies
Know the answer? Post it — somebody with the same question will find it here.