Grails Cookbook - A collection of tutorials and examples

Grails Example Application - Save Documents To Database

This is an example application that implements a simple document management system that saves the file to the database instead of the file system. The goal is to show how to store and retrieve binary data from the database. This is based on a previous post. It is recommended to view that post first before proceeding.
The user interface and user experience is exactly the same as the previous example. Only the internal workings is different:

Listing of all uploaded documents are shown:
e1
Links are provided to download the document file:
e3
Provide a form to upload and add a document to the repository:
e2

Domain

The domain is tweaked from the previous example:
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[].

Listing

We will skip the code for listing as it is exactly the same as the previous example.

Document Upload

The gsp for uploading the file is unchanged, only the controller action is modified:
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()

Document Download

The download code is also simple and shorter.
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.

Remarks

The above code is a modification to the Simple Document Management System Example. By saving the file to the database, the code became shorter and cleaner.
This is however not recommended for real projects as this method is much slower compared to using the file system. Storing files in the database can also explode it's size and may affect your backup and restore strategy.
The full source code for this example can be viewed here or can be downloaded here.

List of Example Applications
  • Simple Ajax Chat
  • Simple Document Management System
  • Save Documents To Database
  • Download / Export Excel File
  • Download / Export ZIP File
  • Grails Forum Application

  • Tags: DMS, download, example, uploadForm