5

I recently migrated from PrimeFaces 7 to PrimeFaces 8, but the p:fileUpload component does not work as expected in PrimeFaces 8. Here is the minimal example:

My facelet:

<!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui"
          lang="en">
          
          <h:head>
           <f:facet name="first">
               <meta http-equiv="X-UA-Compatible" content="IE=edge" />
               <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
               <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
               <meta name="apple-mobile-web-app-capable" content="yes" />
           </f:facet>
          </h:head>
          <h:body>
                <h:outputStylesheet name="primeicons/primeicons.css" library="primefaces"/>
        
                <div class="ui-g ui-fluid"> 
                    <div class="card card-w-title">
                        <h:form id="inseratForm" enctype="multipart/form-data">  
                                <div class="card-title">Objekt anlegen</div> 
                                <p:growl id="messages" autoUpdate="true" showDetail="true" />  
                                <p:fileUpload 
                                        fileUploadListener="#{objAnlBean.handleFileUpload}"
                                        label="Select images" 
                                        auto="true"
                                        multiple="true"
                                        dragDropSupport="true"
                                        sizeLimit="100000000" 
                                        invalidSizeMessage=""
                                        invalidFileMessage="Invalid File"
                                        fileLimitMessage=""
                                        validatorMessage="Invalid File Type"
                                        showButtons="false"
                                        update="@form:uploadedImagesDiv"
                                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/i">
                                        
                                        Upload Images
                                        
                                     <h:panelGroup id="uploadedImagesDiv" layout="block"/>
                                 </p:fileUpload>
                         </h:form>
                    </div>
                </div>      
        </h:body>
       </html>

The Backing Bean:

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@Named("objAnlBean")
@javax.faces.view.ViewScoped
public class ObjAnlBean implements Serializable {

    private static final long serialVersionUID = 1L;

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        String fileName = uploadedFile.getFileName();
        String contentType = uploadedFile.getContentType();
        System.out.println("fileName = " + fileName + ";contentType =  " + contentType);
    }
}


    

My expectation is that whenever I put a breakpoint in the handleFileUpload() method and I try to upload file(s) from the front-end, the handleFileUpload() method is called. This works as expected in PrimeFaces 7, but not in PrimeFaces 8.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Alex Mi
  • 1,409
  • 2
  • 21
  • 35
  • 2
    Compared with the pf showcase? Tried removing all div's? The icons? The meta's? Tried removing attributes from the `p:fileUpload`? Does it start working then? (And adapt the Q accordingly) In the mean time I'll check the migration guide (did you?) – Kukeltje Jun 06 '20 at 07:17
  • 3
    [FileUpload#fileUploadListener has been renamed into listener](https://github.com/primefaces/primefaces/wiki/Migration-Guide) – WoAiNii Jun 06 '20 at 10:30
  • 3
    @WoAiNii is correct `fileUploadListener` is just `listener` now its in the Migration Guide. – Melloware Jun 06 '20 at 11:52
  • @Melloware, thank you, you are right! That solved my problem! Thank you and have a nice day! – Alex Mi Jun 06 '20 at 11:58
  • @WoAiNii thank you, you are right! That solved my problem! Thank you and have a nice day! – Alex Mi Jun 06 '20 at 11:58
  • I would have expected the IDE to have given a warning about this – Kukeltje Jun 06 '20 at 14:19
  • I updated https://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked-or-uploaded to contain the relevant info. – Kukeltje Jun 06 '20 at 14:55

1 Answers1

9

fileUploadListener attribute of the p:fileUpload is renamed in PrimeFaces 8 to just listener

This change is also documented in the Migration Guide.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Alex Mi
  • 1,409
  • 2
  • 21
  • 35