-1

My question is oriented to the process after the file is uploaded, i mean, i'm using JSF 2.1 and also Primefaces to upload files to my project's folder, but i need to get the file immediately recognized by the project so i can use it or read it to show a kind of preview of the file uploaded, so how do i do this?

Pigritia
  • 311
  • 3
  • 10
  • thread [How to use PrimeFaces p:fileUpload? Listener method is never invoked](http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked) and tutorial [Primefaces file upload example](http://www.mastertheboss.com/primefaces/primefaces-file-upload-example) may help you. – Chandra Sekhar Dec 04 '12 at 06:18

1 Answers1

1

Depends on which component you are using. The easiest way would be using Single FileUpload:

<p:fileUpload fileUploadListener="#{fileBean.handleFileUpload}"
                          mode="advanced" 
                          sizeLimit="100000"/>

after uploading the handleFileUpload method will be called:

public void handleFileUpload(FileUploadEvent event) {
    String filename = event.getFile().getFileName();
    String content = new String(event.getFile().getContents());
}

As you can see the filename and the content are both String and it's pretty easy to show some kind of preview after the upload.

For example, you can assign the content to another bean property which is connected to a p:inputTextarea on your interface. Then by using the update attribute of the p:fileUpload you can update that particular p:inputTextarea.

Akos K
  • 7,071
  • 3
  • 33
  • 46
  • that is what i'm doing, but when the file ends up uploading and i try to access it, it does not get recognized, i have to reload the page so i can use it – Pigritia Dec 06 '12 at 18:05