snowindy
snowindy

Reputation: 3251

How to upload files in grails webflow?

I try to build a grails webflow containing a step which has a file upload field in it. Can't find a clear example. How can I do it?

The form looks like this:

<g:form method="POST" action="upload" enctype="multipart/form-data">
    <input type="file" name="myFile" />
    <input type="submit" value="Upload! " />
</g:form>

The flow looks like this:

def rgdpsRequestFlow = {
    chargeCheck{
        on("upload"){ 
            println "Hello file upload"
        }.to("uploadSuccess")
    }
    uploadSuccess{

    }
}

When I submit the form I get

HTTP Status 404 - /webflowTest/mortgage/upload

Upvotes: 1

Views: 5042

Answers (2)

snowindy
snowindy

Reputation: 3251

The problem was I used g:form action attribute. The correct way to use file upload with webflow is using g:submitButton

Here is the form code that works fine with my webflow:

<g:form method="POST" enctype="multipart/form-data">
    <input type="file" name="myFile" />
    <g:submitButton class="save" name="upload" value="Upload!"/>
</g:form>

Upvotes: 1

Igor
Igor

Reputation: 33983

It's a little unclear which part you're struggling with. If you need help with the file upload, you may want to try the Grails File Uploader plugin, or otherwise check out this step-by-step tutorial or use the uploadForm tag that James Kleeh has pointed out.

Namely, in the gsp you can add:

<g:uploadForm controller='yourControllerName' action='save'>
    <input type='file' name='file'/>
    <input type='submit'/>
</g:uploadForm>

Then handle it in the controller:

def save = {
    def file = request.getFile('file').inputStream.text
    file.transferTo(new File('someLocation/filename'))
}

Upvotes: 3

Related Questions