In my MEAN application I need to provide a link to download a file, the link must be hidden and not accessible by unauthorized users.
So I came up with this idea of keeping the files inside the server directory and let Angular.js send with ng-click="download()" an $HTTP request to express.js with the file ID to download, and (possibly) the user id/pwd.
First of all is this a safe solution?
Second, here is my code that doesn't work, there are no errors whatsoever, but I can't even get the download dialog box to open:
Client Side
$scope.download=function(){
$http({method:'GET', url:'/download/'+image[0]['_id']}).
success(function(data, status, headers, config) {
var element = angular.element('<a/>');
element.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
target: '_blank',
download:'test.csv'
})[0].click();
}).
error(function(data, status, headers, config) {
});
}
Server Side
app.namespace('/download/:documentID*', function() {
app.all('/', function(req, res, next){
res.download('images/download/test.tif', 'test.tif', function(err){
if (err) {
} else {
next();
}
});
});
})