0

I need to send JSON to a web service using HTTP POST method in AngularJS to download a file.

AngularJS:-

$http
                .post(
                    'url',
                    'My Json data ')
                .success(function(response) {
                    console.log('file downloading');
                })
                .error(
                    function(response) {
                        console
                            .log('Error while downloading file');
                    });

Spring Controller:-

@RequestMapping(value = "/url", method = RequestMethod.POST)
    @ResponseBody
    public void getfile(@RequestBody List<ABC> abc, HttpServletResponse response)
            throws JRException, IOException, SQLException {
//My code here 
        response.reset();
        response.setContentType("application/x-pdf");
        response.setHeader("Content-disposition", "attachment; filename=ABC.pdf");
        final OutputStream outStream = response.getOutputStream();  
        JasperExportManager.exportReportToPdfStream(jasperPrint,outStream);
        outStream.flush();
        outStream.close();

I have to call this from angular using POST request. How to acheive so?

EDIT I was able to meet the requirements by referring this thread.

Community
  • 1
  • 1
Saumyaraj
  • 1,220
  • 3
  • 15
  • 37

1 Answers1

1

please see this very helpful function to download a file using POST request. Function is dependant on jQuery . the implementation creates a html form inline with hidden field and then submit it

Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Yes, I was able to do that by referring http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit. Thanks for info Nikolay. – Saumyaraj Aug 25 '15 at 07:10