strongloop / loopback

LoopBack makes it easy to build modern applications that require complex integrations.
http://loopback.io
Other
13.22k stars 1.2k forks source link

Issue downloading pdf; file is corrupted #2063

Closed varunj90 closed 7 years ago

varunj90 commented 8 years ago

Hi,

I am receiving a pdf file from the server on the internet. This is in binary format and as soon as I try adding it in a callback, the file gets corrupted/damaged. Whereas if I simply return the file without a cb, it gets downloaded.

_var options = { url: config.baseURL + config.getDocument.url + docId, encoding: null, method: 'GET', qs: {'a': true, 'alf_ticket': ticket} };

// send out request request(options, function(error, response, body) {onGetDocumentResponse(error, response, body, callback);}); } _

In my remote method when I do the following

_ params.res.setHeader('Content-Type', 'application/pdf'); params.res.setHeader('Content-Disposition', 'attachment; filename=' + params.docid + '.pdf');

  return params.res.end(result.body);_

It downloads the file perfectly, but as soon I introduce callback

cb(null,result.body); The pdf received is damaged (size is 195 bytes vs 4kb of original file)

I've read on multiple threads of having issues in downloading pdfs in node. Is there any solution that you could recommend which would allow us to use a callback instead of a return statement (since that would intercept all middleware)

@raymondfeng @richardpringle

lomashg commented 8 years ago

Hi Joe Lomash from TD , i have replaced Varun ( who is not out of a long vacation) . Do let me know any updates on this one .

lomashg commented 8 years ago

@Joesimonsen @bajtos Please provide an update on this feature .

bajtos commented 8 years ago

@lomashg @Joesimonsen I think the current status is that we are still not able to reproduce your problem :( From what I can tell, it looks like some part of the code converts a Buffer instance to a plain object via toJSON, this is happening before the data reaches strong-remoting's toResult() function.

https://github.com/strongloop/loopback/issues/2063#issuecomment-190779380 makes me believe that the part in your application works correctly and provides a Buffer instance. At the same time, I am not able to find out where is this instance would get converted to a JSON-like object, because I don't see any code in strong-remoting that would do that.

@lomashg From my POV, it will be best if you could try to reproduce the issue in a small app based on https://github.com/strongloop/loopback-sandbox, so that we can run the same code on our machines too. You can replace the call of ironMountain.getDocument with code that always returns the same Buffer instance, for example:

Document.getDocumentById = function(id, cb) {
  process.nextTick(function() {
    var content = new Buffer([1,2,3]);
    var contentType = 'application/octet-stream'
    var contentDisposition = 'attachment; filename=' + id + '.bin';
    cb(null, content, contentType, contentDisposition);
  });
};