1

I have seen this same question here, however the solution is not working in my case and inputting the code snipped that was used

$('<form action="'+ url +'" method="'+ ('post') +'">'+inputs+'</form>')
           .appendTo('body').submit().remove();

returns an error that Reference $ is not defined

I have a download request that looks like this:

controller.js

$http.put('/download' {file:$scope.inputInfo.name.$modelValue+'.csv'}).success(function(data,status,headers){
        console.log(status);
        console.log(data);

    })

app.js

app.put('/download', function(req,res){
    console.log(req.body.file)
    res.download(__dirname +'/'+ 'req.body.file, function(err){
      if(err){console.log(err)}
   })

})

My controller.js is returning the raw file output as data and not initiating the download from the page. Is there something I am missing to initiate this file download in angular? Any help would be greatly appreciated.

Community
  • 1
  • 1
MonsterWimp757
  • 1,197
  • 2
  • 14
  • 29
  • 1
    `Reference $ is not defined` means it can't find jQuery at the time of execution. Is that your only error? – Seth Aug 14 '14 at 18:15
  • Yes thats my only error - when I add the jQuery library to my page I just get a blank page returned when I trigger the download – MonsterWimp757 Aug 14 '14 at 19:20
  • the answer that you're getting help from has an action in it's form. Actions redirect when forms are submitted. in your click handler for the submit button, you must pass `e` to the handler's callback function and use the `e.preventDefault()` method inside your callback. – Seth Aug 14 '14 at 21:18
  • This totally helped although I did not need the e.preventDefault(), I posted the answer below. – MonsterWimp757 Aug 14 '14 at 21:37

1 Answers1

1

Seth you totally set me in the right direction with the lack of jQuery on my page.

Here is my finished code that I added to controllers.js in place of the $http.put():

$('<form action="'+ "/download" +'" method="'+ ('post') +'">'+'<input type="hidden" name="file" value="'+$scope.inputInfo.name.$modelValue+'"'+'/></form>')
           .appendTo('body').submit().remove();

Thanks again for your help with this.

Community
  • 1
  • 1
MonsterWimp757
  • 1,197
  • 2
  • 14
  • 29