简体   繁体   中英

How download a file from Amazon S3 with express.js and request.js?

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 !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM