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.

4
  • 1
    Reference $ is not defined means it can't find jQuery at the time of execution. Is that your only error? Commented Aug 14, 2014 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 Commented Aug 14, 2014 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. Commented Aug 14, 2014 at 21:18
  • This totally helped although I did not need the e.preventDefault(), I posted the answer below. Commented Aug 14, 2014 at 21:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome that you figured it out... Glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.