2016-12-27 164 views
0

我试图将图像提交给grails中的特定目录,同时搜索我发现要上传的代码使用以下方法“getFile”,但是此方法不被识别,并且依赖项或导入都没有找到,控制器是这个request.getFile无法解析符号

def uploadImage(){ 
    def file=request.getFile('image') 
    String imageUploadPath=grailsApplication.config.imageUpload.path 
    try{ 
     if(file && !file.empty){ 
      file.transferTo(new File("${imageUploadPath}/${file.name}")) 
      flash.message="your.sucessful.file.upload.message" 
     } 
     else{ 
      flash.message="your.unsucessful.file.upload.message" 
     } 
    } 
    catch(Exception e){ 
     log.error("Your exception message goes here",e) 
    } 

} 

我做了搜索,而这应该是唯一的方法..

+0

不确定您的意思是“找不到依赖或导入”。这是一个注入Grails请求对象的方法,所以你在IDE中通常不会自动完成它。真正的问题是,哪些不起作用?你运行代码时会得到什么错误? – Gregg

+0

运行.getFile时出现错误,是由grails检测到的错误,所以我认为我只需要一个特定的导入或依赖关系的build.gradle – Zenoheld

+0

您需要更具体。什么是错误信息? – Gregg

回答

0

确保您使用<g:uploadForm />为客户端上传,否则,你会在控制器中加载错误的外观。

+0

我不认为你的答案可以应用于不需要'gsp'的REST控制器 – gkiko