image upload using primefaces

Posted: August 27, 2012 in web

add xhtml

<p:fileUpload id=”tmp-image” value=”#{bean.uploadedFile}”
mode=”simple” sizeLimit=”100000″
allowTypes=”/(\.|\/)(gif|jpe?g|png)$/” />

bean

private UploadedFile uploadedFile;

public String saveAd() {

try {
Card card = new Card();
card.setImage(uploadedFile.getContents());
}

public StreamedContent getImage() {
if (facesContext.getRenderResponse()) {
return new DefaultStreamedContent();
}
ByteArrayInputStream imageStream = null;
for (Card card : cards) {
int id = Integer.parseInt(facesContext.getExternalContext().getRequestParameterMap().get(“id”));
if (card.getId() == id) {
imageStream = new ByteArrayInputStream(card.getImage());
}
}
return new DefaultStreamedContent(imageStream);
}

display xhtml

<p:graphicImage value=”#{bean.getImage}”>
<f:param name=”id” value=”#{card.id}” />
</p:graphicImage>

entity

@Column(name = “Image”)
private byte[] image;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s