Oleksandr.Bezhan
New member
Hi. I have an issue displaying an image.
I debugged it and I saw a strange thing: in save() action file size is 10172 bytes, but when I retrieve it from database(showImage() action) it's size is 20343. It's almost twice largest!
Maybe somebody can help me. Please, look at my code.
I have a domain class Image:
I save this image in ImageController in the following way:
I have next action in controller to display an image:
And finally I retrieve an image from my GSP like this:
I debugged it and I saw a strange thing: in save() action file size is 10172 bytes, but when I retrieve it from database(showImage() action) it's size is 20343. It's almost twice largest!
Maybe somebody can help me. Please, look at my code.
I have a domain class Image:
Code:
class Image {
byte[] file;
String name;
String alt;
String title;
String contentType;
static constraints = {
name(nullable: false, blank: false)
file(nullable: false, maxSize: 1024 * 1024 * 2)
contentType(nullable: false, validator: {val, obj ->
return allowedContentTypes.contains(val);
})
}
static allowedContentTypes = ['image/png', 'image/jpeg', 'image/gif']
}
I save this image in ImageController in the following way:
Code:
def save() {
def image = new Image(params)
def file = ((MultipartRequest)request).getFile('file')
image.contentType = file.getContentType()
image.file = file.getBytes()
if (!image.save(flush: true)) {
render(view: "create", model: [image: image])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'image.label', default: 'Image'), image.id])
redirect(action: "edit", id: image.id)
}
I have next action in controller to display an image:
HTML:
def showImage() {
def image = Image.get(params.id)
response.contentType = image.contentType
response.contentLength = image.file.size()
OutputStream out = response.getOutputStream();
out.write(image.file);
out.close();
}
Code:
<img src="${createLink(controller: 'image', action: 'showImage', id: image.id)}"/></g:link>