0

I know my question is quite basic but I couldn't find anything on SO or Google regarding this.

I am using PrimeFaces and their p:fileUpload component. It is working fine, files are being uploaded. The problem is, after these files are uploaded, I need to show user how many files are uploaded, and not using JavaScript. I need this number in my Backing Bean.

There is one oncomplete attribute but that is for client side callback (which again is JavaScript).

Following is my humble code :)

<p:fileUpload required="true" requiredMessage="No files selected."
              mode="advanced"
              multiple="true"
              dragDropSupport="true"
              allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
              update="growlMessage"
              fileUploadListener="#{mainForm.fileUploadListener}">
</p:fileUpload>

And here is backing bean:

@ManagedBean
@SessionScoped
public class MainForm {
    private int totalImageFiles;

    public MainForm() {
        this.totalImageFiles = 0;
    }

    public void fileUploadListener(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        ReaderWriter.ReadWrite(uploadedFile, yourName, yourHomeAddress, totalImageFiles);
        totalImageFiles++;
    }
}

totalImageFiles is the number that I want to show user. It has the correct values but I don't know how to send another request to retrieve this number.

Bugs Happen
  • 2,169
  • 4
  • 33
  • 59

1 Answers1

2

Just reference it in some output component and ajax-update it.

<p:fileUpload ... update="growlMessage totalImageFiles" />
<h:outputText id="totalImageFiles" value="#{mainForm.totalImageFiles}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have used the exact thing you asked and it has started working. But I am not ajax-updating anything. Will that be problem in future? – Bugs Happen Sep 02 '15 at 15:34
  • Also, now that it is showing, `totalImageFiles` is also not accurate. Sometimes it shows correct number and other times not. Do you think my above approach for calculating total uploaded files is correct? – Bugs Happen Sep 02 '15 at 15:57
  • The `update` attribute does that. As to the misnumbering, the cause is not visible in the information provided so far. – BalusC Sep 02 '15 at 18:25
  • Please check the updated code. Now can you see some problem regarding misnumbering? – Bugs Happen Sep 02 '15 at 20:36
  • The bean being session scoped is wrong. When you refresh the page or open the same URL in a new tab/window in the same browser session, the same bean instance is reused. It should be view scoped instead. See also http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – BalusC Sep 02 '15 at 21:11
  • Sorry for late reply, I have tried `@ViewScoped` and it didn't help either. Total files are 18 and it shows 17. – Bugs Happen Sep 05 '15 at 08:25
  • Searching "jsf count file upload" on Google gives this link's question. And it didn't have that answer. Can you please help us out? – Bugs Happen Sep 07 '15 at 14:50
  • Perhaps `ReaderWriter.ReadWrite` threw an exception which went unnoticed? That really can't be answered without debugging yourself. – BalusC Sep 07 '15 at 17:24
  • `ReaderWriter.ReadWrite` is fine, checked. While debugging, on its first iteration when it receives first file, it adds 1 in `totalImageFiles` but on its second iteration, it receives 4th or 5th file and `totalImageFiles` becomes 5 or 6. – Bugs Happen Sep 07 '15 at 19:03
  • If its not too much trouble, can I ask you to try the above code yourself, see the behaviour? Maybe things are fine but I am missing some important concept regarding JSF. – Bugs Happen Sep 07 '15 at 19:05