0

I am trying to use JSF 2.2 new to let the user upload a photografy to his profile. Anyway I need an Ajax behavior, which I achieved with the following snippet:

<h:form enctype="multipart/form-data">
    <h:inputFile value="#{usuarioController.part}">
        <f:ajax listener="#{usuarioController.uploadImage}"/>
    </h:inputFile>
</h:form>

But at the moment my public void uploadImage() the javax.servlet.http.Part part still null..

This is the controller:

@Named(value="usuarioController")
@SessionScoped
public class UsuarioController extends GenericPersonificacaoCrudController<Usuario>{
    private static final long serialVersionUID = 3233882970467365819L;
    private Part part;

    public void uploadImage(){
        System.out.println(part);
    }

    public Part getPart() {
        return part;
    }

    public void setPart(Part part) {
        this.part = part;
    }
}

I am using Mojarra 2.2.6 implementation of JSF with Tomcat + Weld CDI and Primefaces 5.1 which is unrelated to the question since I am using the native fileUpload component, but I am including just to let you know I also tried using it and it doesnt work with the mode="advanced" which use ajax, what make me wonder if it is some kind of incompatibility or conflict of the libraries I am using.

Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78
  • This might be related http://stackoverflow.com/questions/16336795/jsf-2-2-fileupload-does-not-work-with-ajax-form-appears-to-have-incorrect-enc – kolossus Nov 21 '14 at 20:54

1 Answers1

0

You're trying to access the file using the java representation of the <h:inputFile/> component; you should not be doing this. As you have it, file should be bound to an instance of javax.servlet.http.Part, from which you can get an InputStream. The rest is quite straightforward from that point. Your code would look something like this:

public Part file;
//getter and setter

public void uploadImagem(){

    long fileSize = file.getSize();
    byte[] fileStorage = new byte[fileSize];
    BufferedInputStream bis = new BufferedInputStream(file.getInputStream);
    bis.read(fileStorage);

    //do whatever you want with the array.

}

Tip: You should also know that the Part also has a convenience method write() that allows you to write the file to a specified directory, directly

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Thanks for your reply. Anyway my file which is an instance of javax.servlet.http.Part is null at moment my uploadImagem method is triggered.. Any clue? Thanks again @kolossus – Marcos J.C Kichel Nov 21 '14 at 15:59
  • Where is the surrounding `` @MarcosJ.CKichel ? Post it here – kolossus Nov 21 '14 at 16:00
  • It is at the template, and yes I did put the enctype="multipart/form-data" attribute at it, I also overrided all the template to test, and it still dont work.. I am using mojarra 2.2.6 with tomcat 8.. @kolossus – Marcos J.C Kichel Nov 21 '14 at 16:03
  • You'll need to update your question with enough sample code to attempt to reproduce the problem @MarcosJ.CKichel . Also look in your javascript console to ensure that the part is being transferred from your browser – kolossus Nov 21 '14 at 16:06