【发布时间】:2014-04-04 07:35:11
【问题描述】:
我的目标:
显示一个对话框,提示用户保存从 aws 下载的文件。
我的问题:
我目前正在使用 awssum-amazon-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!');
});
};
此代码的目的是让用户使用生成的密钥下载文件一次。
【问题讨论】:
标签: node.js amazon-web-services amazon-s3 download disk