I guess you did something like this:
UI-Dialog:
p:row>
p:column>
p:fileUpload label="Upload" fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"
auto="true" />
/p:column>
/p:row>
Due to this is a primeface-action there should be no problem with this.
Your Java-Class:
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
@ManagedBean
@ViewScoped
public class UploadBean {
public void handleFileUpload(FileUploadEvent event) {
UploadedFile uploadedFile = event.getFile();
byte[] myFile= uploadedFile.getContents();
}
The byte[] you now can save to your database as bytea.
![How it works][1]
[1]: http://answers.axonivy.com/upfiles/primefaces.png
In case you wonder how the persistence could be implemented:
Dataclass:
@Entity
@Table(name = "myFunny_FilesTableName")
@Access(AccessType.PROPERTY)
public class Files implements Serializable {
private Long id;
private static final long serialVersionUID = 1L;
private byte[] myPdf;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Lob //this annotation is only nescessary if you want it to save as a large object in your //database. database. If you use @Lob you //you get a postgres oid. If you just send the byte[] without annotation you get a bytea.
public byte[] getMyPdf() {
return myPdf;
}
public void set(MyPdf(byte[] myPdf){
//do sth
}
Persistence class:
public class FilesDao {
public Files saveOrUpdate(Files files) throws Exception {
IIvyEntityManager entityManager= Ivy.persistence().get(myDatabaseContextName);
Files newFiles;
try {
newFiles = entityManager.merge(files);
} catch (Exception e) {
Ivy.log().error("Unable to save file.");
throw e;
}
return newFiles;
}
And now you can add these line to save your files:
byte[] pdfByteArray; //the byteArray from the primefaces
Files myPdf= new Files(); //create a new entity
myPdf.setMyPdf(pdfByteArray) //fill it with your data
Files thePersistedPdf= new FilesDao().saveOrUpdate(myPdf); //merge it to your databse.
Hope this helps a bit.