I have the following class:
class Exibitions {
String name
String location
String description
Date dateStart
Date dateEnd
byte[] flier
static mapping = {
flier sqlType:'longblob' //use mysql
}
static constraints = {
description nullable: true
flier nullable: true
}
}
The admin can upload the flier (a PDF file) with this form:
<g:if test="${exibitionInstance?.flier}">
Inserted
</g:if><g:else>
<g:form action="uploadFlier" id="${exibitionInstance.ident()}" enctype="multipart/form-data">
Insert Flier (PDF):
<input type="file" id="flier" name="flier" />
<g:actionSubmit class="create" action="uploadFlier" value="Carica" />
</g:form>
</g:else>
The controller code handling the upload is the following:
def uploadFlier(){
Exibition exibitionInstance = Exibition.get(params.id)
def file = request.getFile('flier')
if(file.empty) {
flash.message = "File cannot be empty"
} else {
exibitionInstance.flier = file.getBytes()
if(exibitionInstance.save()){
println 'SAVED!'
}
if(exibitionInstance.flier==null){
flash.message = "Flier not uploaded"
}else{
flash.message = "Flier uploaded"
}
}
redirect (action:'index')
}
The controller prints "SAVED" and give me the flash message "Flier uploaded" and i can print the contents of the flier longblob attribute.
But when controller ends, i have a NULL in the flier column in my database:
UPDATE:
I figured it out. I could insert the pdf from mySql workbench setting the column manually. Then i could download the pdf from grails. So the bug had to be in the controller. The controller method uploadFlier() was not @Transactional (and read-only). After the change i got the error: com.mysql.jdbc.PacketTooBigException: Packet for query is too large.
After the fix everthing works.