Dany Y
Dany Y

Reputation: 7041

Grails 3 multipart request with Spring Security

I have a Grails 3 rest application, with a call to save uploaded files.

def saveAll() {
    request.fileNames.each { filename ->
        if(!filename.empty){
            File file = params[filename];
            file.transferTo(grailsApplication.config.filesPath)
        }
    }
}

The problem is that the code works correctly, except when I have the control secured with Spring Security, then the params are empty and I find no alternative

(I know that with Spring security there is a wrapper for request, and found several with this issue but still didn't find a clear solution)

Upvotes: 2

Views: 423

Answers (2)

Dany Y
Dany Y

Reputation: 7041

The solution was to actually add @Transactional to the function. I have no idea why this is the case. but it worked this way

Upvotes: 1

Omar Yafer
Omar Yafer

Reputation: 863

have you tried using request.getFiles() instead of request.getFileNames(). I have an app that uses springSecurityRest plugin and is secured using annotations and part of the working code is similar to this.

if(request instanceof  MultipartHttpServletRequest){
        request.getFiles().each { MultipartFile file ->
            // ... the code to transfer the file goes here 
        }
    }

I´ve experimented, and in controllers that inherit from RestfulController, it seems that the data of the request is flushed once it is bound. This might be the reason why your params are appearing to be null. I don´t know if it applies here, or if I´m correct in my assumption, though.

Upvotes: 0

Related Questions