im getting this error "image corrupt or truncated" on FF console when I try to display an image stored in my database. I´m using Grails and Hibernate. I just want to display a user´s profile picture in my gsp page.
My domain class User has an attribute photo defined as byte[]
public class User {
private String userId;
private byte[] photo;
.
.
.
The user.hbm.xml mapping is :
< property name="photo" type="binary" column="PHOTO" not-null="false" />
This collumn is a BLOB on DB.
My gsp:
< div id="user-view-thumbnail-container">
<fieldset>
<legend>Photo Upload</legend>
<g:form action="uploadPhoto" method="post" enctype="multipart/form-data">
<label for="photo">Photo</label>
<input type="file" name="photo" id="photo" />
<input type="submit" class="buttons" value="Upload" />
</g:form>
</fieldset>
<g:if test="${user.photo}">
<img alt="User Photo" src="${createLink(controller:'profile', action:'profile_image', id:user.userId)}" />
</g:if>
</div>
ProfileController:
def uploadPhoto = {
def user = userService.getUser(authenticateService.principal())
def f = request.getFile('photo')
user.setPhoto(f.getBytes())
if (!user.save()) {
//doesn´t matter now
return;
}
redirect(action: 'index', model:[user: user])
}
def profile_image = {
def user = userService.getUser(params.id)
if (!user) {
response.sendError(404)
return;
}
response.setContentType(user.photo.contentType())//'image/png', 'image/jpeg', 'image/gif'
response.setContentLength(user.photo.size())
OutputStream out = response.getOutputStream();
out.write(user.photo);
out.close();
}
FYI: The picture is saved on DB correctly, I can see it, but I can´t show it on my gsp.
Any idea?
Thanks a lot guys,
Use blob type in the mapping :).