0

I tried to get and parse an XML file sent an upload from the GSP view. The file is correctly sent by the client but when I tried to parse it I have an exception.

------WebKitFormBoundaryPTpPKdL3WQWaPzJp--
 *(The filename, directory name, or volume label syntax is incorrect). Stacktrace follows:
Message: ------WebKitFormBoundaryPTpPKdL3WQWaPzJp
Content-Disposition: form-data; name="file"; filename="myfile.xml"*

gsp:

<form enctype="multipart/form-data" method="post" action="uploadXml">
    <input type="file" name="file" id="file" />
    <input type="submit">
</form>

controller:

def project = new XmlParser().parse(request.reader.text)

On using request.getFile('file'):

MultipartFile file = request.getFile('file')

I have another exception:

*| Error 2015-03-30 09:46:41,328 [http-bio-8080-exec-5] ERROR errors.GrailsExceptionResolver  - MissingMethodException occurred when processing request: [POST] /BOSWEBConfigurator/BOSOrder/uploadXml
No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [file]
Possible solutions: getXML(), getPart(java.lang.String), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(). Stacktrace follows:
Message: No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [file]
Possible solutions: getXML(), getPart(java.lang.String), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON()*
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Jils
  • 783
  • 5
  • 12
  • 32

2 Answers2

0

You can use XmlSlurpr (See this question Slurpr vs Parser)

In your controller

def uploadXml() {
    MultipartFile file = request.getFile('file')
    GPathResult xml = new XmlSlurper().parse(file.inputStream)
}

And then you can process xml as you wish.

You can use g:uploadForm tag to generate a multipart form if you like.

Make sure you dont have this config set to true grails.web.disable.multipart=true

Here's the spring security issue which you have encountered

Community
  • 1
  • 1
Sudhir N
  • 4,008
  • 1
  • 22
  • 32
  • I looked jira: I set grails.web.disable.multipart to false, then I added @Secured(['ROLE_ADMIN']) at the top of the controller, but I still have the exception – Jils Mar 30 '15 at 09:12
0

After invesigation, I noticed that I didn't added Spring Security statis rules in the Config.groovy:

grails.plugins.springsecurity.controllerAnnotations.staticRules = [
'myController/uploadXml' :['ROLE_ADMIN'']
]

Now its work

Jils
  • 783
  • 5
  • 12
  • 32