仅将文件上载到特定参数

时间:2015-04-07 17:13:57

标签: grails file-upload

我正在将文件上传到列表中。但是我希望能够指定我上传哪个参数,以便下次访问时我只看到上传到该路径的文件:

例如我有这个下拉菜单:

<ul class="dropdown-menu" role="menu">
    <li><g:link controller='Document' action='list' params='[fruit:"apple"]'>apple</g:link></li>
    <li><g:link controller='Document' action='list' params='[fruit:"blueberry"]'>bluberry</g:link></li>
    <li><g:link controller='Document' action='list' params='[fruit:"strawberry"]'>strawberry</g:link></li>
    <li class="divider"></li>
    <li><g:link resource="gm">v4sa</g:link></li>
</ul>

如果我点击苹果。我上传了一个文件。我希望能够仅为params.fruit == apple

查看这些文件

仅在此网址中表示:http://localhost:8080/file_down/document/list?fruit=apple

如果我转到蓝莓的网址,我也可以上传文件,但在列表中我只能看到为蓝莓上传的文件:

以下是我的上传控制器:

class DocumentController {

    def index = {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = 10
        [documentInstanceList: Document.list(params), documentInstanceTotal: Document.count()]
    }


    def upload() {
        def uploadedFile = request.getFile('file')
        if(uploadedFile.isEmpty())
        {
            flash.message = "File cannot be empty"
        }
        else
        {
            def documentInstance = new Document()
            documentInstance.filename = uploadedFile.originalFilename
            //fileSize
            documentInstance.fileSize = uploadedFile.size
            documentInstance.fileSize = documentInstance.fileSize/(1024*1024)
            documentInstance.fullPath = grailsApplication.config.uploadFolder + documentInstance.filename
            uploadedFile.transferTo(new File(documentInstance.fullPath))
            documentInstance.save()
        }
        redirect (action: 'list')
    }
}

这是我的list.gsp,显示表中的文件,它是所有不同参数的相同表,同一个gsp页面,只显示不同的文件:

<tbody>
    <g:each in="${documentInstanceList}" status="i" var="documentInstance">
    <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
        <td><g:link action="download" id="${documentInstance.id}">${documentInstance.filename}</g:link></td>
        <td><g:link id="${documentInstance.id}">${documentInstance.fileSize}MB</g:link></td>
        <td><g:formatDate date="${documentInstance.uploadDate}" /></td>
        <td><span class="button"><g:actionSubmit class="delete" controller="Document" action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" /></span></td>
    </tr>
    </g:each>
</tbody>

如何仅上传到一个特定参数并仅在该参数中显示它?

修改 这是我的上传表格:

<g:uploadForm action="upload" enctype="multipart/form-data">
                <fieldset class="form">
                    <input type="file" name="file" />
                </fieldset>
                <fieldset class="buttons">
                    <g:submitButton name="upload" class="save" value="upload" />
                    <g:hiddenField name="fruit" value="${params.fruit?: 'defaultValue'}" />
                </fieldset>
            </g:uploadForm>

1 个答案:

答案 0 :(得分:1)

如果您按表单上传文件,请添加名为fruit的隐藏字段:

<g:hiddenField name="fruit" value="${params.fruit ?: 'defaultValue'}"/>

如果是ajax,那么添加数据参数:

data:{file:file, fruit:"${params.fruit ?: defaultValue}",..

defaultValue需要解决错误。祝你好运:)