• Post Reply Bookmark Topic Watch Topic
  • New Topic

Message not displaying after downloading a file

 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day guys,

I have a page that I want to debug, but its hard, so i need to help from u.

The codes is like this:

public void processPage() {

MessageUtil.addErrorMessage("Error Message");

downloadPDF();

return null;

}



private void downloadPDF() {
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setContentLength(pdf.length);

List<String> hwbs = ePODRequest.getHwbs();
String fname = utilsFactory.getPDFFilename(hwbs);

StringBuilder sb = new StringBuilder("attachment; filename=\"")
.append(fname)
.append("\"");
String removeNewLine = sb.toString().replaceAll("\\r|\\n", "");

response.setHeader("Content-disposition", removeNewLine);
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
LOGGER.error("Unable to print to outputstream", e);
}
faces.responseComplete();
}


THE question is why the Message not appear when downloadPDF() was called?
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I often say, the more JSF-specific code you write, the more likely you're not doing it right. I recommend writing a standard (non-JSF) servlet or JSP when you need to return a non-HTML file to the client.

But the real limitation here is in HTTP itself, not in JSF or J(2)EE.

In HTTP, there is a strict 1-to-1 request/response architecture. You send one request, you get back one response. In this particular case, you send a request for a file, you get back an outputstream containing the file's contents. There's no room in HTTP for also sending back a confirmation message in the same request/response cycle.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the advice..

The problem is, it is already written, i just need to fix it. So any idea on how can I send back a message?
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if I remember corrrectly, the "Save As" dialog box that the client pops up won't appear until the download has completed already anyway.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks.

Can you ellaborate on how to do that?

Really thanks
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no "how to do that". It's done automatically by the client.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean what will I do?

Use a dialog box to render message?
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dialogbox is the message.

Like I said, only after the data has downloaded will the client ask where to store it. Or, alternatively, what program to run against it, depending on MIME settings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!