I have implemented a code to download .doc file from server and as a server I am using node.js with express my server side code look like
function downloadresume(req, res, next)
{
var data=req.body.data;
console.log(data);
try {
var JsonData=JSON.parse(JSON.stringify(data));
console.log(JsonData);
var filename=JsonData.Id+".docx";
var file=__dirname+"/Resumes/"+filename;
fs.exists(file, function(exists) {
if (exists) {
var stats = fs.statSync(file);
var mimetype = mime.lookup(__dirname+"/Resumes/"+filename);
res.setHeader('Content-disposition', 'attachment; filename='+filename);
res.setHeader('Content-type', mimetype);
return res.download(file);
} else {
res.send([{error:'File not exists'}]);
}
});
} catch (e) {
console.log(e);
res.send(e);
}
}
and React Side code look like
DownloadResume:function(id)
{
var postdata={
data:{Id:id}
};
try {
$.ajax({
url:config.APIURL+"downloadresume",
type: "POST",
data:JSON.parse(JSON.stringify(postdata)),
success: function (data, textStatus, jqXHR) {
if(data !==null && typeof data[0] !== 'undefined')
{
swal("error",data[0].error,"error");
}
},
error: function(xhr){
swal("error",'Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText,"error");
}
});
} catch (e) {
swal("Eroor",e, "error");
}
},
it's not working if I will call downloadresume direct from browser then file is download successfully but when I tried it from React it's not working can anyone please guide me what's going wrong in above mentioned code.
I got the below mentioned response when I called it