views:

1227

answers:

2

Hi,

I have an action class that generates pdf. The contentType is set appropriately.

public class MyAction extends ActionSupport 
{
   public String execute() {
    ...
    ...
    File report = signedPdfExporter.generateReport(xyzData, props);

    inputStream = new FileInputStream(report);
    contentDisposition = "attachment=\"" + report.getName() + "\"";
    contentType = "application/pdf";
    return SUCCESS;
   }
}

I call this action class through an Ajax call. I don't know the way to deliver this stream to browser. I tried a few but nothing worked.

    $.ajax({
              type: "POST",
              url: url,
              data: wireIdList,
              cache: false,
              success: function(response)
              {
                 alert('got response');
                 window.open(response);
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('Error occurred while opening fax template' 
                      + getAjaxErrorString(textStatus, errorThrown));
              }
         });

The above gives error

Your browser sent a request that this server could not understand.

Please opine. Thanks

A: 

Do you have to do it with Ajax? Coouldn't it be a possibility to load it in an iframe?

Emil Vikström
I am checking if, this could be done with Ajax. If it is technically impossible or an inferior approach, i'd switch to other approaches.
Nayn
+1  A: 

You don't necessarily need Ajax for this. Just an <a> link is enough if you set the content-disposition to attachment in the server side code. This way the parent page will just stay open, if that was your major concern (why would you unnecessarily have chosen Ajax for this otherwise?). Besides, there is no way to handle this nicely acynchronously. PDF is not character data. It's binary data. You can't do stuff like $(element).load(). You want to use completely new request for this. For that <a href="pdfservlet/filename.pdf">pdf</a> is perfectly suitable.

To assist you more with the server side code, you'll need to tell more about the language used and post an excerpt of the code attempts.

BalusC
content-disposition to attachment is not working with Ajax. I can't give a hyperlink since the document is being dynamically generated on server side.
Nayn
Once again: you **do not** need Ajax for this. It's only asking for trouble. PDF is binary data, not character data like HTML or JSON.
BalusC
var url = contextPath + "/xyz/blahBlah.action"; url += url + "?" + params; try { var child = window.open(url); child.focus(); } catch (e) { }
Nayn
Done it as above.
Nayn
In some browsers the window.open will stay open and blank, which may be annoying for endusers. So, also do NOT use window.open for this. If the `content-disposition` is set to `attachment`, you will just get a `Save as` dialogue. The parent page will stay unchanged. Just `<a href="pdfservlet/filename.pdf">pdf</a>` or a `<form action="pdfservlet/filename.pdf"><input type="submit"></form>` is more than enough.
BalusC
I have a button, onclick event I decide if I should make an ajax call to do some action XYZ or show the pdf. I tried window.location.href but it didn't work in firefix. any idea what is the right way to do this?
Nayn