1

I am creating a file on server in a request from client-side. Now, I want to send that file in response to the AJAX response. Below is the JAVA code.

response.reset();
            response.setContentType("application/pdf"); 
            response.setHeader("Content-disposition", "attachment; filename=\"Portfolio.pdf\""); 
            OutputStream output;
            try {
                output = response.getOutputStream();
                output.write(Util.readFileInBytes("/Portfolio.pdf"));
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

Now, how to show a "Save As" dialog to user for saving the file. Thanks in advance for the help.
The javascript code is as below :

$.ajax({
            url : "export",
            dataType : 'text',
            contentType : 'application/pdf',
            success: function() { //code to display "Save As" dialog box to user.}});
smk
  • 5,340
  • 5
  • 27
  • 41
Poonam Waikul
  • 151
  • 2
  • 10
  • See http://stackoverflow.com/questions/12876000/how-to-build-pdf-file-from-binary-string-returned-from-a-web-service-using-javas – guest271314 Aug 27 '15 at 03:18
  • Thanks for the help. I implemented the solution given in the above URL. But, it is creating a new window with nothing inside. The new window is blank. – Poonam Waikul Aug 27 '15 at 05:28
  • _"it is creating a new window with nothing inside. The new window is blank."_ can create stacksnippets , jsfiddle.net to demonstrate ? See also http://stackoverflow.com/questions/28207106/pdf-file-upload-ajax-html , http://stackoverflow.com/a/23987178/ . – guest271314 Aug 27 '15 at 13:38

1 Answers1

0

Also see this discussion stackoverflow.com/questions/833068/how-to-force-save-as-dialog-box-in-firefox-besides-changing-headers.

You can encode file in base64 string, send it to client and then assign the value to location.href and save dialog will be shown to user.

But it won't work in IE. Don't know what are your requirements.

Community
  • 1
  • 1
Alex Barkun
  • 503
  • 6
  • 13