Reputation: 210
I am using PrimeFaces.Monitordownload function to monitor the progress of file download. First the file is created then downloaded. If anything goes wrong, a error file is created and downloaded with streamed content instead of real file. This is fine and works well. The problem occurs when I want to add a error message along with the error file. I have no idea how to update my growl.
When using PrimeFaces.monitorDownload, I need to set ajax=false
. Could this be part of my problem?
I have tried multiple things, like setting update="growl"
on the p:commandButton
, autoUpdate=true
on the growl itself, but nothing. I might be doing something wrong in my backing bean, but don't think so.
Here is my relevant Java code:
boolean fileCreated = processor.start();
if(fileCreated){
File xlsfile = new File(filePath);
InputStream stream = new FileInputStream(xlsfile);
file = new DefaultStreamedContent(stream, "file/xlsx", fileName);
return file;
}else{
fileName = fileName.replace(".xlsx", ".txt");
filePath = rootPath + fileName;
File xlsfile = new File(filePath);
InputStream stream = new FileInputStream(xlsfile);
file = new DefaultStreamedContent(stream, "file/txt", fileName);
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Something went wrong!", "Please send an email to servicedesk and alert the administrators of this page!");
FacesContext.addMessage(null, message);
return file;
}
And my xhtml:
<p:dialog modal="true" widgetVar="statusDialog" header="Status"
footer="Creating file. It might take up to 30 minutes, depending on the file size."
draggable="true" closable="false" resizable="false">
<p:graphicImage name="/bigLoader.gif"
style="margin-left :auto; margin-right:3.7cm;" />
</p:dialog>
<p:commandButton id="submitBtn" value="submit" ajax="false"
disabled="#{controllerBean.selectedBrands.size() lt 1}"
styleClass="btn btn-default"
onclick="PrimeFaces.monitorDownload(start, stop);">
<p:fileDownload value="#{controllerBean.file}" />
</p:commandButton>
<p:commandButton value="Add brand" styleClass="btn btn-default"
action="#{controllerBean.addBrand()}"
update=":first:brands: :first:tableGroup: :first:submitBtn:">
</p:commandButton>
I'm using Mojarra 2.2, Primefaces 5.3. Any help much appreciated!
Upvotes: 0
Views: 1662
Reputation: 339
Filedownload is a non-ajax request, so the update attribute won't do anything.
I think you don't need to download a fake/blank file in the case of an error. You should just present the user an error msg.
I recommend that you create 2 steps to download, as your file generation process seems too slow:
First step (ajax) generates the file and saves it on the backing bean (if successful). If the generation fails, then present error message.
If the file was successfully generated (fileCreated=true), enable the download button, which will just download the generated file (ajax=false).
Remember to nullify the file variable after download to free memory.
Additionally, if you have the time, you could put a progressBar for the generation process...
Upvotes: 1