Goro
Goro

Reputation: 31

Primefaces fileupload works only for the first file to be uploaded

I am using Primefaces and I have an issue with the fileuploader. The problem is that it works fine with the first file I upload (the fileUploadListener is called and the java method uploads the file), but then if I try to upload another file, the listener is not called anymore and the java method is never triggered. If I refresh the page, I can upload another file but again if I try to upload a second one the same thing happens.

code within the xhtml file:

<p:tab id="eConsentTabPanel" title="Informed Consent" disabled="#{patientHandlerAction.patientTabsStatus}">
  <h:form id="eConsentForm" enctype="multipart/form-data">
     <h:panelGrid id="eConsentPanelGrid">
        <p:fileUpload fileUploadListener="#{patientHandlerAction.handleFileUpload}" 
                      mode="advanced"
                      update="messages"
                      multiple="true"
                      auto="true"
                      sizeLimit="20971520"
                      label="Select File"
                      allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc?x)$/"/>
        <p:growl id="messages" showDetail="true"/>  
     </h:panelGrid>
  </h:form>  
</p:tab>

code within the java bean file:

public void handleFileUpload(FileUploadEvent event) {

   ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
   String filename = FilenameUtils.getName(event.getFile().getFileName());
   File result = new File(extContext.getRealPath("//uploaded//" + filename));

   try {
      FileOutputStream fileOutputStream = new FileOutputStream(result);

      byte[] buffer = new byte[6124];

      int bulk;
      InputStream inputStream = event.getFile().getInputstream();
      while (true) {
         bulk = inputStream.read(buffer);
         if (bulk < 0) {
            break;
         }
         fileOutputStream.write(buffer, 0, bulk);
         fileOutputStream.flush();
      }

      fileOutputStream.close();
      inputStream.close();

      messageTitle = "Message";
      messageBody = "The file" + event.getFile().getFileName() + " was succesfully uploaded!";

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, messageTitle, messageBody));

   }

   catch (IOException e) {
      e.printStackTrace();

      messageTitle = "Message";
      messageBody = "The file was not uploaded";

      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, messageTitle, messageBody));
      FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The files were not uploaded!", "");
      FacesContext.getCurrentInstance().addMessage(null, error);
   }

}

code within the web.xml file:

<filter>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <filter-class>
      org.primefaces.webapp.filter.FileUploadFilter
   </filter-class>
   <init-param>
      <param-name>uploadDirectory</param-name>
      <param-value>/HerEhrUploaded/temp</param-value>
   </init-param>
   <init-param>
      <param-name>thresholdSize</param-name>
      <param-value>20971520</param-value>
   </init-param>      
</filter>

<filter-mapping>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Upvotes: 3

Views: 3865

Answers (3)

Khaidar Rapiz Putra
Khaidar Rapiz Putra

Reputation: 31

I added :

event.getComponent().setTransient(false);

Inside file upload listener method,

public void handleUpload(FileUploadEvent event) {
    ....
}

It's works fine.

Upvotes: 3

mipi
mipi

Reputation: 11

I had the same problem and I found a solution (more workaround than a solution) here:

http://code.google.com/p/primefaces/issues/detail?id=6157

It seems to be a problem with isTransient() method in the FileUpload component. Hope it helps.

Upvotes: 1

Goro
Goro

Reputation: 31

I think I have found what's wrong but I am not sure of what is causing this issue. The xhtml page that I use to display the fileUpload component, has many tabs and each tab has it's own h:form. I created a new page that has only one form and the fileUpload component seems to work fine there.

Upvotes: 0

Related Questions