简体   繁体   English

如何使用express.js和request.js从Amazon S3下载文件?

[英]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. 我尝试使用pipe方法从Amazon S3使用express.js和request.js下载文件。

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"? 但是,当我转到: localhost:3000 / download时 ,浏览器会下载一个名为“ download”的文件,我该如何设置浏览器要下载的文件名,例如“ 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. HTTP有一个名为Content-Disposition的标头,可用于指定文件名。 Here's your program setting it to download to myFile.zip: 这是您将其设置为下载到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 资料来源: https : //stackoverflow.com/a/1741508/2329281

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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