How to save an upload file to the bank with Primefaces

0

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?

    
asked by anonymous 06.02.2018 / 03:05

1 answer

0

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; } }

    
06.02.2018 / 14:57