2

I am try download a file from Amazon S3 with express.js and request.js using the pipe method.

But when I go to: localhost:3000/download the browser download a file with the name of "download" How I can Set the file name for the browser to download such as "myFile.zip"?

This is my code:

var express = require('express');
var app = express();
var request = require('request');

app.get('/download', function(req, res){
  request('https://s3-us-west-2.amazonaws.com/bucket-name/object.zip')
  .pipe(res.set('Content-Type', 'application/zip'))
});

app.listen(3000);

Any help is going to be appreciated !

Louis
  • 146,715
  • 28
  • 274
  • 320

1 Answers1

4

HTTP has a header called Content-Disposition that allows you to specify a filename. Here's your program setting it to download to myFile.zip:

var express = require('express');
var app = express();
var request = require('request');

app.get('/download', function(req, res){
  request('https://s3-us-west-2.amazonaws.com/bucket-name/object.zip')
  .pipe(res.set('Content-Type', 'application/zip').set('Content-Disposition', 'inline; filename="myFile.zip"')
});

app.listen(3000);

Source: https://stackoverflow.com/a/1741508/2329281

Community
  • 1
  • 1
Piper McCorkle
  • 1,044
  • 13
  • 27