---
title: "Grails结合uploadify上传文件的简单代码"
author: "rox"
date: "2010-12-06T15:25:29Z"
tags: ["CSS", "Flash", "Grails", "JavaScript", "jQuery"]
categories: ["编程语言"]
slug: "grails-uploadify"
draft: false
lastmod: "2010-12-06T15:25:29Z"
id: 834595
url: "https://rox.iteye.com/blog/834595"
---

参考了这篇[the](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [script](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [is](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [not](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [working](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [for](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [any](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [mvc](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)[](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA) [framework](http://www.google.com/url?q=http%3A%2F%2Fwww.uploadify.com%2Fforums%2Fdiscussion%2F285%2Fthe-script-is-not-working-for-any-mvc-framework%2F&sa=D&sntz=1&usg=AFQjCNEs7yuuDyAVLXzxkDUrjwB7NlJ5qA)中uri的回复，结合当前Uploadify的v2.1.4版，netbean 6.9.1下编写调试。

●        新建grails工程，名为test，并把Uploadify-v2.1.4.zip解压到web-app\\uploadify目录下。

●        新建grails控制器PictureController，代码如下

package com.test

 

class PictureController {

 

    def index = { redirect(action: "upload") }

 

    def upload = { }

 

    def savePicture = {

       def files = request.getFileMap();

       def path = "c:/test/" + files.get("Filedata").getOriginalFilename();

       files.get("Filedata").transferTo( new File(path))

       render (view:'upload')

    }

}

●        新建grails视图upload.gsp，代码如下，注意粗体部分的上下文路径

&lt;html&gt;

  &lt;head&gt;

    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;

    &lt;title&gt;upload file sample&lt;/title&gt;

    &lt;link rel="stylesheet" href="**/test/uploadify/uploadify.css**" type="text/css" /&gt;

    &lt;script type="text/javascript" src="**/test/uploadify/jquery-1.4.2.min.js**"&gt;&lt;/script&gt;

    &lt;script type="text/javascript" src="**/test/uploadify/swfobject.js**"&gt;&lt;/script&gt;

    &lt;script type="text/javascript" src="**/test/uploadify/jquery.uploadify.v2.1.4.min.js**"&gt;&lt;/script&gt;

    &lt;script type="text/javascript"&gt;

    $(document).ready(function() {

            $("#fileUpload").uploadify({

                'uploader': '**/test/uploadify/uploadify.swf**',

              'cancelImg': '**/test/uploadify/cancel.png**',

              'script': '**/test/picture/savePicture**',

              'folder': 'files',

              'multi': true,

              'buttonText': 'add',

              'displayData': 'speed',

              'simUploadLimit': 2

            });

    });

    &lt;/script&gt;

  &lt;/head&gt;

  &lt;body&gt;

    &lt;hr width=100% size="1" color="" align="center"&gt;

        &lt;h2&gt;Multiple File Upload&lt;/h2&gt;

        &lt;p&gt;checkScript, buttonText, simulataneous upload limit&lt;/p&gt;

        &lt;div id="fileUpload"&gt;You have a problem with your javascript&lt;/div&gt;

        &lt;a href="javascript:$('#fileUpload').uploadifyUpload()"&gt;Start Upload&lt;/a&gt; |  &lt;a href="javascript:$('#fileUpload').uploadifyClearQueue()"&gt;Clear Queue&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

  &lt;/body&gt;

&lt;/html&gt;

●        运行工程，访问http://localhost:8080/test/picture/upload