I researched a bit about uploading files with primefaces and found no code example that saved a file in the database.
Can you save a file as if it were an attribute of a class?
I researched a bit about uploading files with primefaces and found no code example that saved a file in the database.
Can you save a file as if it were an attribute of a class?
Give yes, you have to save a byte array of the file. I used the example of the primefaces component ( link ) as a reference:
Entity:
public class Pessoa {
private Integer idPessoa;
private String nome;
private byte[] foto;
//get e set
}
ManagedBean:
@ManagedBean(name = "pessoaMB")
@SessionScoped
public class PessoaMB {
private Pessoa pessoa;
private StreamedContent foto;
@PostConstruct
public void postConstruct() {
this.pessoa = new Pessoa();
}
public void handleFileUpload(FileUploadEvent event) {
try {
this.pessoa.setFoto(event.getFile().getContents());
this.foto = new DefaultStreamedContent(event.getFile().getInputstream());
FacesMessage message = new FacesMessage("Sucesso", event.getFile().getFileName() + " foi carregada.");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (IOException e) {
e.printStackTrace();
}
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public StreamedContent getFoto() {
return foto;
}
}