p:filedownload from p:datatable with ViewScoped managed bean wont work. It calls the methods prepareFile and getFile twice. In first call of the methods I mentioned it sets the first file from the table, and in the second call of the methods it sets the right file, but it always downloads only the first one and the second one is never downloaded.
Why does it call twice? Why does it set the first file from the table? Any ideas?
Here's my code:
<p:dataTable id="offer_attachment_datatable"
widgetVar="offer_attachment_datatable"
var="attachment"
value="#{offerBean.offerAttachments}">
<p:column>
<f:facet name="header"/>
<p:commandLink ajax="false" actionListener="#{offerBean.prepareFile(attachment)}" title="#{attachment.name}">
<p:graphicImage value="/resources/themes/navigator_b2e/images/drive-download.png" />
<p:fileDownload value="#{offerBean.file}"/>
</p:commandLink>
</p:column>
</p:dataTable>
and in managed bean (simplified):
private StreamedContent file;
private InputStream stream;
public void prepareFile(OfferAttachment attachment){
System.out.println("Attachment: "+attachment.getName());
stream = new ByteArrayInputStream(attachment.getAttachment());
file = new DefaultStreamedContent(stream, "text/plain", attachment.getName());
stream = null;
}
public StreamedContent getFile() {
System.out.println("File: "+file.getName());
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
So, I made a workaround with a simple p:confirmDialog where I extracted the problematic ajax=false command link, so I select the attachment by clicking it in p:datatable and execute the download from the p:confirmdialog.
I had the same problem in 2.2.1. I found the solution by replacing p:commandLink
to p:commandButton
with the same attributes. Seems that it is a bug related with behavior of the commandLink
component
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With