我还是Grails
的新手。我有一个file upload
的应用。我已将此添加到控制器
def upload = {
def f = request.getFile('myFile')
if(!f.empty) {
f.transferTo( new File("${f.name}") )
response.sendError(200,'Done');
} else {
flash.message = 'file cannot be empty'
render(view:'uploadForm')
}
}
,这是_forms.gsp
<div class="fieldcontain ${hasErrors(bean: reportInstance, field: 'myFile', 'error')} ">
<label for="myFile">
<g:uploadForm action="upload">
<input type="file" name="myFile"/>
<input type= "submit" value="Upload"/>
</g:uploadForm>
</label>
当我使用g:link
尝试检索上传时,我会重定向并且没有显示任何内容
任何建议都将不胜感激 g:我正在使用链接
<g:link controller ="Report"
action="listByUserCompany"
>Show first ten ordered by Title</g:link>
答案 0 :(得分:1)
class UploadController {
def index() { }
def fileupload () {
def f = request.getFile('myFile')
if(!f.empty) {
f.transferTo( new File("${f.name}") )
render(view:'upload')
}
else {
flash.message = 'file cannot be empty'
render(view:'upload')
}
}
}
<body>
<g:uploadForm name="myUpload" action="fileupload" controller="upload">
<input type="file" name="myFile" />
<input type= "submit" value="Upload"/>
</g:uploadForm>
</body>
我在我的控制器和gsp中使用了上面的代码,我在view->upload->upload.gsp
中创建了一个名为upload的视图。我的文件上传成功完全在项目本身的根目录中,名称为myfile