0

I am trying to get downloading files from S3 via node/knox working. My javascript call is working and successfully downloads the file, but I want to download it as an attachment. I have tried setting the headers to 'Content-disposition': 'attachment; filename=myfile.zip', but it doesn't seem to be working. Here is my sample code:

var mimetype = mime.lookup(product.filename);           
var headers = {
    'Content-disposition': 'attachment; filename=' + product.filename,
    'Content-type': mimetype
};

var get = knox.getFile(product.filename, function(err, result){
    if(err) { return next(err); }   

    res.setHeader('Content-disposition', 'attachment; filename=' + product.filename);
    res.setHeader('Content-type', mimetype);

    result.pipe(res);           
});

I have also tried setting those headers on the knox call, but still won't download as attachment.

ben75
  • 29,217
  • 10
  • 88
  • 134
Matt Way
  • 32,319
  • 10
  • 79
  • 85
  • Try changing "Response-Content-disposition" to just "Content-disposition"? AFAIK the former is not a real header. – sgress454 Feb 28 '14 at 19:19
  • sorry I was tired, I have tried the correct header. – Matt Way Feb 28 '14 at 23:27
  • Looks right to me. What's happening instead, is it trying to display the bytes in the browser? – sgress454 Feb 28 '14 at 23:56
  • 1
    Yeah. If I `console.log` the result, it just shows the bytes. I am using angular, so if you think this is looks ok, I wonder if it is client code related? – Matt Way Mar 01 '14 at 01:00

1 Answers1

1

So it looks like the problem wasn't my server at all, as I was unaware that you cannot use xhr (i.e. $resource with Angular) to download files as attachments. The simplest way I have found to get around this so far, is to only use xhr to validate the download, returning a token to the user which can be used non-xhr to get the actual file.

Matt Way
  • 32,319
  • 10
  • 79
  • 85