I have a view scoped bean where I create a person. A person can have a picture. This picture is uploaded the same page the person is created. The picture is not stored in a
I managed to do that by simply encoding uploaded image to base64 and then showing it normally via html tag.
Here's my managed bean:
@ManagedBean
@ViewScoped
public class ImageMB {
private String base64Image;
public void onUploadImage(FileUploadEvent event) {
String fileName = event.getFile().getFileName();
//Get file extension.
String extension = "png";
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i + 1).toLowerCase();
}
String encodedImage = java.util.Base64.getEncoder().encodeToString(event.getFile().getContents());
this.base64Image = String.format("data:image/%s;base64, %s",
extension, encodedImage));
}
And here's JSF part: