为什么我在Grails中找不到上传的文件

时间:2013-12-03 05:02:30

标签: grails file-upload upload gsp

我还是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>

1 个答案:

答案 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