Scenario:
I need to send a user file that is remotely stored on a server (cloud) in a browser. I can request an image from the server and then transfer it to the browser, but it does not work 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); }); }
The above code gives an empty PDF file for the client, whereas if I open the cloud URL directly, then it fits correctly. I rack my brains for a long time, but I canβt get any of this.
I really appreciate your help.
Edit: I also found this similar question: The express server sends an empty PDF file , but this also does not work for me. I tried disabling connnect-livereload, but even then it does not work.
The problem occurs only for PDF files. All other formats work fine.
dark_shadow
source share