package asia.grails.dbdms class Document { String filename byte[] filedata Date uploadDate = new Date() static constraints = { filename(blank:false,nullable:false) filedata(blank: true, nullable:true, maxSize:1073741824) } }Instead of having the fullPath on where the file is stored in the file system, the binary data is stored as a table column. The type in Java of the column is byte[].
We will skip the code for listing as it is exactly the same as the previous example.
package asia.grails.dbdms class DocumentController { def upload() { def file = request.getFile('file') if(file.empty) { flash.message = "File cannot be empty" } else { def documentInstance = new Document() documentInstance.filename = file.originalFilename documentInstance.filedata = file.getBytes() documentInstance.save() } redirect (action:'list') } }As you could see, the example is much simpler. Just assign the bytes of the file to the Domain field. documentInstance.filedata = file.getBytes()
package asia.grails.simpledms class DocumentController { def download(long id) { Document documentInstance = Document.get(id) if ( documentInstance == null) { flash.message = "Document not found." redirect (action:'list') } else { response.setContentType("APPLICATION/OCTET-STREAM") response.setHeader("Content-Disposition", "Attachment;Filename=\"${documentInstance.filename}\"") def outputStream = response.getOutputStream() outputStream << documentInstance.filedata outputStream.flush() outputStream.close() } } }Just retrieve the byte data from the field and send in to the response object outputStream << documentInstance.filedata.