我的目标:NodeJS如何从aws s3存储桶下载文件到磁盘?
显示一个对话框,提示用户保存从aws下载的文件。
我的问题:我目前使用的awssum - 亚马逊S3来创建一个下载流
。不过,我只设法将文件保存到我的服务器或流到命令行...正如你可以从我的代码看到我的最后一次尝试是尝试和手动设置失败的内容处置标题。我不能使用res.download()作为标题已被设置?
我该如何实现目标?
我的节点代码:
app.post('/dls/:dlKey', function(req, res, next){
// download the file via aws s3 here
var dlKey = req.param('dlKey');
Dl.findOne({key:dlKey}, function(err, dl){
if (err) return next(err);
var files = dl.dlFile;
var options = {
BucketName : 'xxxx',
ObjectName : files,
};
s3.GetObject(options, { stream : true }, function(err, data) {
// stream this file to stdout
fmt.sep();
data.Headers['Content-Disposition'] = 'attachment';
console.log(data.Headers);
data.Stream.pipe(fs.createWriteStream('test.pdf'));
data.Stream.on('end', function() {
console.log('File Downloaded!');
});
});
});
res.end('Successful Download Post!');
});
我对角码:
$scope.dlComplete = function (dl) {
$scope.procDownload = true;
$http({
method: 'POST',
url: '/dls/' + dl.dlKey
}).success(function(data/*, status, headers, config*/) {
console.log(data);
$location.path('/#!/success');
}).error(function(/*data, status, headers, config*/) {
console.log('File download failed!');
});
};
这段代码它的目的是让用户使用一个生成的密钥下载一个文件一次。
不幸的是,你不能下载通过AJAX请求将文件发送到用户磁盘(请参阅[此处](http://stackoverflow.com/a/9970672/2137601)和[there](http://stackoverflow.com/a/14683228/2137601)) )。你可以做的是让用户用'dlKey'数据发送POST FORM。 –