I think you are making this too complicated. All you should need to do is 
to create a link to the file (place the URL you're using in the AJAX 
request in the href of the link instead) and then have your Express code 
"strongly encourage" the client to download the file rather than display 
it. Take a look at 
https://stackoverflow.com/questions/10615797/utility-of-http-header-content-type-application-force-download-for-mobile
 
for some information. Basically, add the Content-Disposition: attachment; 
etc etc header to the response with the file. Don't forget that just 
because a user can't see a link, that doesn't mean they can't access a URL. 
This means that you need to validate on the server side that they are 
authorized to access the file even if they were able to ping the URL.

On Monday, April 14, 2014 9:14:51 AM UTC-6, Smart Cris wrote:
>
>
> This time I got bad luck with Stack Overflow, you guys are my last 
> resource, and pls be kind with a newbie. Here is the trouble: in my MEAN 
> application I need to provide a link to download a file (tif image), the 
> link must be hidden and not accessible by unauthorized users. So I came up 
> with the 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. 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/tif;charset=utf-8,' + encodeURI(data),
>          target: '_self',
>          download:'test.tif'
>      })[0].click();
>    }).
>   error(function(data, status, headers, config) {
>    });
>   }
>
> Template:
>
> <a ng-href="#" target="_self" type="button" class="btn" 
> ng-click="download()">Download</a>
>
> 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();
>       }
>     });
>   });})
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to