Tomás Lin’s Programming Brain Dump

Flex, Grails, Facebook, iPhone and all that Jazz

A Simple Grails Controller for File Management

with 7 comments

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 -

Written by Tomas Lin

November 26, 2008 at 1:46 am

Posted in Uncategorized

Tagged with ,

7 Responses

Subscribe to comments with RSS.

  1. Neat. I might integrate this into a future version of grails.org to provide better support for files uploads

    Graeme Rocher

    November 26, 2008 at 4:44 am

  2. 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

    Carl

    November 26, 2008 at 6:05 am

  3. Hi Carl, I fixed the style issue but am able to upload perfectly. This is on Grails 1.0.4

    Tomas Lin

    November 26, 2008 at 11:22 am

  4. Thanks Thomas – That did the trick!

    Thanks

    Carl

    November 26, 2008 at 12:18 pm

  5. [...] ตัวอย่างนี้ใช้ controller ตัวเดียวกับ view เป็น .gsp อีกไฟล์สร้างตัวจัดการไฟล์อย่างง่ายที่ upload, ดูและลบไฟล์ได้ มีคำอธิบายและตัวอย่าง application ที่พร้อมรันไว้บนหน้าเวบแล้วครับ [...]

  6. 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

    Jonathan

    December 30, 2008 at 2:43 pm

  7. 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:

    Anon Omas

    February 19, 2009 at 11:31 am


Leave a Reply