5

I can get the raw data of the file that I am requesting, but I can't get the browser to serve the file to the user. Do I need to use iframes?

   //Client code
   download_file: function (path, callback) {
       $http.post('/download/client_file', {path:path}).
           success(function(data, status, headers, config) {
                console.log(data); //this contains the raw data of the res.download 
                                   //from the server.
           });
   }

   //server code
   res.download(file); // the path is proper
The_Brink
  • 2,649
  • 2
  • 16
  • 18

2 Answers2

2

I used the code below in my services file for downloading items and it worked great. I got it from http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/

$('<form action="'+ url +'" method="'+ ('post') +'">'+inputs+'</form>')
               .appendTo('body').submit().remove();
The_Brink
  • 2,649
  • 2
  • 16
  • 18
  • 1
    I would +2 this if I could. Awesome solution. One thing to clarify in this answer is that `inputs` might look like this: `` where `mydata` is some object from your `$scope`. See my full example on my blog: http://jessemon.blogspot.com/2013/11/download-data-from-angular-nggrid-as-csv.html – Jess Nov 06 '13 at 10:34
  • I am running into this same problem - I have a controller that is doing a $http.put() to my main app.js. The data response I get is also the raw output of my .csv file and replacing that with the above is not giving me `Reference Error: $ is not defined` is there something I am missing? – MonsterWimp757 Aug 14 '14 at 17:37
  • 1
    @MonsterWimp757 If you want to use $ check if you have loaded jquery before angular at index.html – Eric Aug 05 '16 at 05:36
0

Depending on the file type, and how the headers are set on it, you may find it vastly easier to simply call location.href="uriString"; from your JS. File should download immediately, using the window you're already in. If it's JSON or text data, you may need to tweak the headers to make it download rather than rendering inline... ("content-disposition: attachment", perhaps...)

XML
  • 19,206
  • 9
  • 64
  • 65