0

My Code In gsp:

<html>
  <body>
    <g:form controller="some_controller" action="some_action" enctype="multipart/form-data">
       <input id="Resume" type="file" name="Resume" />
    </g:form>
  </body>
</html>

My controller code :

def candidate = new Candidate(params)
MultipartHttpServletRequest mhsr = (MultipartHttpServletRequest) request
MultipartFile candResume = mhsr.getFile('candResume')

if(userService.fileExt().contains(candResume.getContentType()))
 {
 candidate.candResume=candResume.getBytes() //Converting a file into bytes
 if(candidate.validate())
   {
   if(candidate.save(flush: true,failOnError: true))
    {
     println "++++++++++Candidate Success+++++++++++++"
     flash.candSuccess="Candidate successfully added."
    }
   }
   else{
     println "====Sorry Candidate Upload Failed===="
     flash.candFail="Candidate failure."
   }
}

My service code :

public List fileExt(){
        List fileExtensions=["doc", "docx", "pdf", "rtf"]
        println "--------in the service----------"
        return fileExtensions
    }

The thing is in if(userService.fileExt().contains(candResume.getContentType())) the service is getting invoked but not retiurning anything the upload is getting failed without any message. Please help. Thanks in advance.

Ishaan Nigam
  • 83
  • 1
  • 10

2 Answers2

0

I would probably do something like this :

In controller:

def userService
def candidate = new Candidate(params)
  def resumeFile = request.getFile('candResume')

  if (userService.allowedExtension(resumeFile)) {
    blablabla

in Service:

Class UserService {

  def allowedExtension (aFile) {
    List fileExtensions=["doc", "docx", "pdf", "rtf"]
    println "--------in the service----------"
    def extension = aFile?.originalFilename?.substring(aFile?.originalFilename?.lastIndexOf(".") + 1);
    return fileExtensions.contains(extension); // if you rely on the actual file name
  }
}
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
0

I recomend you to use mimetype instead extension of files.

Here you find how to get the mimetype of a file using Tika: Getting MimeType subtype with Apache tika

Community
  • 1
  • 1
jpozorio
  • 622
  • 6
  • 7