2

In a one page a have a link to download a file like \myapp\controller\file?id=45

In controller have

        InputStream stream = null;          
        byte [] buffer =someService.getFile(somedata); 
        stream = new ByteArrayInputStream(buffer);
        System.out.println("get file---");
         response.addHeader("Content-Disposition","attachment; filename=report.pdf");

        try {
            IOUtils.copy(stream, response.getOutputStream());
                System.out.println("get file---OK");
        } catch (Exception e) {
            e.printStackTrace();
        }

This work well, but in another page

         $.ajax({
                type: 'POST',
                url: '${url}',                  
                data: {sdate: $('#date').val() },
                success: function (data) {
                    alert(data)                     
                },
                error:function (xhr, ajaxOptions, thrownError) {
                    console.log("in error");
                } 
            });

In console ever say

get file---

get file---OK

but the browser does nothing.

I add in succes alert(data) to debug, it shows the binary content of PDF file like:

%PDF-1.4 %���� 3 0 obj <>stream x�͝O�$�q���)ޑ:��U]���E�d�A��1^h��ǃ\nS��#��������?���X+��RI��J����Y�r��F�?

Now, how explorer understand is a file to download?

For lack of permits, i can not save files server side

jasilva
  • 730
  • 3
  • 17
  • 45

1 Answers1

4

You can't use ajax to download files.

Either use

Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49