David Brewer
David Brewer

Reputation: 1974

How do I get results (successful/not successful) from a g:uploadForm in a GSP file?

I'm trying to get the results from a g:uploadForm action I have in my GSP file. I need to upload the file, then after it's successfully uploaded I need to tell if the upload was successful, then display another area in the GSP file.

            <g:uploadForm action="save" method="post">
            <h1>
                <g:message code="upload"/>
            </h1>
            <h5>
                <g:message code="upload.message"/>
            </h5>
            <br/>
            <input type="file" id="Upload" name="Upload" class="input"/>
            <br/>
            <g:formButton type="submit" buttonClass="btn-primary btn-lg" buttonTextCode="upload.button" />
            </g:uploadForm>

I just need something to say if it was successful or not.

Is this something I need to handle in the controller and just post to the GSP after that? I'm new to grails and groovy.

Upvotes: 0

Views: 85

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

It's pretty common to use flash scoped variables for these types of messages. In fact, if you look at the Grails documentation about uploading files you will see it does just that.

def upload() {
    def f = request.getFile('myFile')
    if (f.empty) {
        flash.message = 'file cannot be empty'
        render(view: 'uploadForm')
        return
    }
    f.transferTo(new File('/some/local/dir/myfile.txt'))
    redirect(render: 'uploadForm')
}

Using the above example you could then include the following in your uploadForm GSP page.

${flash.message} to display this.

Upvotes: 1

Related Questions