A Simple Grails Controller for File Management

Using Grails as a CMS, often times you want your users to add and remove files to a page similar to the way assets are attached to a page in the Confluence Wiki. I recently had to write a controller to handle files upload and download, and thought it might be interesting to share this since it is done so often in the Grails world.

fileupload

Download the controller : FileResourceController.groovy
Download the view : list.gsp
Download complete sample app : uploader.zip
( to run: install grails. Invoke grails run-app. Navigate to http://localhost:8080 )

Usage- copy the list.gsp into your grails-app/views/fileResource/ directory, and your controller into grails-app/controllers

Config- Define a variable called images.location in conf/config.groovy

There are three methods in the controller:

  • list -> gets a list of files = File.list()
  • upload -> uploads a file chosen by the user
  • delete -> removes chosen file = File.delete()

There are some choices made in the views

  • The path to the file was written into a textarea to facilitate copy and paste.
  • The views preview in a new window using HTML links to maximize cross browser compatibility.
  • File upload goes first, since we don’t know how many items are in this list.

What’s could be improved?

  • A more robust solution would integrate both a hierarchical directory structure or allow you to navigate up and down directories or specify an initial directory.
  • Version control for files would be nice, similar to the grails.org wiki.
  • Ideally, updates to this list would be AJAX based so you can include this page into a modalbox
  • Pagination and sorting of files based on file properties.

Reference –

13 thoughts on “A Simple Grails Controller for File Management

  1. Carl

    Hi,

    I get the following error just when I try to upload a file! Not sure if it’s because I’m running on a Mac?

    Message: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {“fileUpload”}
    Caused by: groovy.lang.MissingMethodException: No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {“fileUpload”}
    Class: FileResourceController
    At Line: [29]
    Code Snippet:
    29: def f = request.getFile(‘fileUpload’)
    30: if(!f.empty) {

    Also, I don’t see a the styled version of the upload page!

    Regards,

    Carl

    Reply
  2. Pingback: เกรลส์ หกสิบหก » สร้างตัวจัดการไฟล์ง่าย ๆ ด้วยเกรลส์

  3. Jonathan

    Hi,
    I just playing around with file upload, grails and photos in facebook through flex.
    First time i upload a file (or many files at once) it works perfectly, but when i try to upload another file(s), almost always, i get an exception
    “[105132] errors.GrailsExceptionResolver
    java.lang.NullPointerException
    at java.net.URLDecoder.decode(URLDecoder.java:119)”, that fires an error on the client (Flex). And i have another problem with this, sometimes i choose a file (after i’ve uploaded some) new files (or requests) come with old data, i mean, old file.

    I’ve read about new flash security policy and they say it’s just about writting a crossdomain.xml file at server. But i’ve tried everything and nothing seems to work.
    I am sure it is not a grails problem. Tell me if you want to see some code, grails or flex one.

    Thank you very much. Greetings from Cali, Colombia

    Reply
  4. Anon Omas

    I got the same error as Carl (No signature of method: org.mortbay.jetty.Request.getFile() is applicable for argument types: (java.lang.String) values: {”fileUpload”}) until I added action=”upload” to the File Upload form in list.gsp, like so:

    Reply
  5. parag

    Dear,
    I m grails user. we use flex-scaffold in grails project..Did u know how to upload image (when i m using flex-scafflod plugin) n how to change the name of image while saving…..thanks in advance

    regards,
    parag

    Reply
  6. Pingback: Grails Controller for File Management - Grails Write File

  7. Priya R

    Hi….

    I downloaded “uploder” and tried running it on cmd prompt… It Runs, doesnt give any error. But it stops saying running grails application. Not giving me the browse option “http://localhost:8080/uploader”. When i opened the browser “http://localhost:8080/uploader”, it is giving could not connect to localhost:8080. can anyone give me solution to this?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s