Scenario:
I have to send user file which is stored remotely in a server(cloudinary) to browser. I'm able to request an image from server and then pipe it to browser but it's not working for other files like pdf, excel sheet, etc.
exports.fetchFile = function(req, res, next){
var fileUrl = "/" + req.params.fileUrl;
console.log(fileUrl);
async.auto({
'A': function(callback) {
var queryObj = { 'myUrl' : fileUrl};
// DB Query to retrieve user file information like url, type, etc.
UserFileCtrl.fetchUserFile(queryObj, callback);
}
}, function(err, results){
if(err) {
return res.send(500, "error");
}
var secureUrl = results['A']['secureUrl'];
var fileType = results['A']['fileType'];
// this will be "application/pdf" for pdf
res.setHeader('Content-Type', fileType);
request(secureUrl).pipe(res);
});
}
Above code gives an empty pdf file to client whereas if I open directly the cloudinary url then it's coming properly. I'm breaking my head for a long time but not able to get something out of it.
I really appreciate your help.
Edit: I found this similar question also: Express server send empty PDF file but this is also not working for me. I tried disabling connnect-livereload but even then it's not working.
The issue is happening only for pdfs. All other formats are working fine.