Haythem Nasri
Haythem Nasri

Reputation: 67

Cannot convert class org.primefaces.model.file.UploadedFilesWrapper to interface org.primefaces.model.file.UploadedFile

I'm trying to upload files via primefaces 10.0.. I got the below error msg : "Cannot convert org.primefaces.model.file.UploadedFilesWrapper@7200b7ba of type class org.primefaces.model.file.UploadedFilesWrapper to interface org.primefaces.model.file.UploadedFile"

code as below :

XHTML:

<h:form enctype="multipart/form-data">
    <p:fileUpload uploadLabel="aa" cancelLabel="aa" value="#{managedBean.fileToUpload}" 
              mode="simple" skinSimple="true" multiple="true" />
    <p:commandButton action="#{managedBean.uploadFileDone()}" value="upload" /> 
 </h:form>

ManagedBean:

public void uploadFileDone(){
        System.out.println(fileDetailActivite);
        this.upload(fileDetailActivite,"Activite_"+activite.getId()+".pdf");
    }
    
    
    
    
     public void upload(UploadedFile file,String type)  {
            if (file != null) {
            //  System.out.println("FileUploadEvent");
                
                String path = "C:\\tmp\\";
                File dir = new File (path);
                dir.mkdirs(); 
                
                FacesMessage msg = new FacesMessage("Success! ", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                // Do what you want with the file
                try {
                    InputStream  
                            
                        inputStream =  file.getInputStream();       
                    copyFile(file.getFileName(),  inputStream,type);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                FacesMessage message = new FacesMessage("Successful", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }
        }
         
        
                        
        public void copyFile(String fileName, InputStream in,String type) {
        //  System.out.println("copyFile");
            try {
     
                // write the inputStream to a FileOutputStream
                
                
           fileName=" ";
               // OutputStream out = new FileOutputStream(new File(destination + type+"_"+fileName));
                OutputStream out = new FileOutputStream(new File(destination +type+""+fileName));
                int read = 0;
                byte[] bytes = new byte[1024];
     
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
     
                in.close();
                out.flush();
                out.close();
     
              System.out.println("New file created!");
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

Upvotes: 1

Views: 677

Answers (1)

BalusC
BalusC

Reputation: 1109665

When using multiple="true", you need a UploadedFiles property instead of a UploadedFile property so that it can hold multiple files instead of only one.

private UploadedFiles files;
<p:fileUpload value="#{bean.files}" multiple="true" />

Or, if that was not your intent, then you need to remove the multiple="true" attribute.

private UploadedFile file;
<p:fileUpload value="#{bean.file}" />

Upvotes: 1

Related Questions