TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ravi sharma
NA
87
16.6k
Unable to store server side response file
Aug 3 2018 9:00 AM
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 =
'<html><head>'
;
table_body +=
'<style>table {border-collapse: collapse;border-radius:6px;width:90%;font-family:Trebuchet MS;} .rd{font-weight:bold;} .rd {font-weight: bold; font-size:18px;background:#2980B9;color:#fff}'
;
table_body +=
' .r1 {background-color:#F6F6F6;color:#666666;}'
;
table_body +=
' .r2 {background-color:#E9E9E9;color:#666666;}'
;
table_body +=
'td {border: 1px solid grey;text-align: left;padding: 4px;}'
;
table_body +=
'table.print-friendly tr td, table.print-friendly tr th {page-break-inside: avoid;}'
;
table_body +=
'</style></head><body>'
;
table_body +=
'<center><h1>Appuser List</h1>'
;
table_body +=
'<div class="table-responsive">'
;
table_body +=
'<table class="table">'
;
var
str_Fields = fields.split(
','
);
table_body +=
'<tr class="rd">'
;
for
(i = 0; i < str_Fields.length; i++) {
if
(i % 6 == 0)
{
table_body +=
'</tr><tr class="rd">'
;
}
table_body +=
'<td>'
+ str_Fields[i] +
'</td>'
;
}
table_body +=
'</tr>'
;
for
(
var
i = 0; i < arrData.length; i++) {
var
cssclass =
''
;
if
(i % 2 == 0) {
cssclass =
'r1'
;
}
else
{
cssclass =
'r2'
;
}
table_body +=
'<tr class="'
+ cssclass +
'">'
;
var
k = 0;
for
(
var
index
in
arrData[i]) {
if
(k % 6 == 0) {
table_body +=
'</tr><tr class="'
+ cssclass +
'">'
;
}
table_body +=
'<td>'
;
table_body += arrData[i][index];
table_body +=
'</td>'
;
k++;
}
table_body +=
'</tr>'
;
}
table_body +=
'</table></div></center></body></html>'
;
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 => {
});
},
Reply
Answers (
0
)
how to solve 503 Service Unavailable in heroku server
angucomplete-alt.js