gpt4 book ai didi

grails - 仅将文件上传到特定参数

转载 作者:行者123 更新时间:2023-12-02 15:24:54 24 4
gpt4 key购买 nike

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

例如,我有这个下拉菜单:

<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的那些文件

这意味着仅在此URL中: http://localhost:8080/file_down/document/list?fruit=apple

如果我转到blueberry的URL,我也可以上传文件,但是在列表中,我只能看到为bluberry上传的文件:

这是我的上传 Controller :
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>

最佳答案

如果您通过表格上传文件,请添加带有水果名称的隐藏字段:

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

如果通过ajax,则添加到数据参数中:
data:{file:file, fruit:"${params.fruit ?: defaultValue}",..

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

关于grails - 仅将文件上传到特定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com