【发布时间】:2017-01-13 15:10:01
【问题描述】:
我目前正在尝试从我的 mongo DB 下载文件。 开始下载后,我可以在 chrome 开发工具的网络选项卡中看到文件正在下载。一旦它在这里完成,它就会下载到本地驱动器。我真的不知道怎么跳过这个直接下载到本地驱动。
这对于用户体验来说是非常糟糕的,因为文件非常大而且似乎什么都没有发生。
服务器端:
app.get('/download/single',function(req,res){
gfs.findOne({ _id: req.query.targetFile}, function(err,file){
if (err) {
return res.status(400).send(err);
}
else if (!file) {
return res.status(404).send('Error on the database looking for the file.');
}
else{
res.set('Content-Type', "application/vnd.android.package-archive")
var readstream = gfs.createReadStream({
_id: req.query.targetFile,
})
readstream.pipe(res)
readstream.on('end',function(){
res.end()
})
}
})
})
客户端:
app.service('Download',function($http){
this.single = function(id){
return $http({
method: 'GET',
url: '/download/single',
params: {
targetFile: id
},
transformResponse: [function (data) {
return data;
}]
}).success(function(response){
return response
})
}
})
app.controller('downloadCtrl',function($scope,$routeParams,$window,Download){
Download.single($routeParams.id).success(function(data){
if(data){
var blob = new Blob([data], {
type: "application/vnd.android.package-archive"
})
saveAs(blob,'test.apk',true)
}
})
})
【问题讨论】:
标签: javascript angularjs mongodb file express