-
1. Re: How to download dynamicly created file?
kenfinni Feb 21, 2013 9:00 AM (in response to mbasovni)1 of 1 people found this helpfulI'm confused as to whether you mean download from the browser to server or download a file generated by server to the user in the browser?
The stackoverflow links you provided refer to the latter, so I will assume that scenario.
You should be able to add an f:ajax inside the h:commandLink and that will convert the portal url to be a ResourceRequest instead of an ActionRequest.
-
2. Re: How to download dynamicly created file?
mbasovni Feb 21, 2013 2:51 PM (in response to kenfinni)Thank you for your answer, Ken! Now it WORKS but I did it a little bit differently.
<a4j:mediaOutput element="a" cacheable="false" createContent="#{calendarController.exportCalendar}"
value="#{event.calendar}" mimeType="text/Calendar">
<h:outputText value="#{event.calendar.name}" />
</a4j:mediaOutput>
public void exportCalendar(OutputStream os, Object calendar) {
... // handle calendar to outputStr
FacesContext fc = FacesContext.getCurrentInstance();
Object response = fc.getExternalContext().getResponse();
...
ResourceResponse rresp = (ResourceResponse) response;
rresp.reset();
rresp.setContentType("text/Calendar");
rresp.setProperty("Content-disposition", "attachment; filename=\"" + filename + "\"");
...
try {
os.write(outputStr.getBytes(Charset.forName("UTF-8")));
os.flush();
} finally {
os.close();
}
...
}
I did not use command fc.responseComplete();, is it ok?
2nd possible solution:
When I tried the way similar to http://stackoverflow.com/questions/10451095/downloading-a-csv-file-using-jsf again with "<h:commandLink ....><f:ajax /></h:commandLink>",
I have now ResourceResponse but file was not returned to user. My code was very similar to the code in this URL, but instead of HTTPServletResponse I used object of type ResourceResponse. Do you know why it does not work? There is no exception after running my code...
But the first solution works! I am just interested in why the second solution does not work...
Thx!
-
3. Re: How to download dynamicly created file?
kenfinni Feb 22, 2013 9:07 AM (in response to mbasovni)Glad to hear it works.
I would think you do need to call responseComplete() on FacesContext, otherwise the remainder of the JSF lifecycle will execute when you don't want it to.
As for why the 2nd solution doesn't work, I'm not certain what the issue might be, as it should work.
-
4. Re: How to download dynamicly created file?
mbasovni Feb 23, 2013 7:24 AM (in response to kenfinni)Ok, thank you very much, Ken!