Multiple file upload in grails

Posted By : Shiv Kumar | 22-Dec-2014

Many times we have to upload multiple files along with form data in many registration forms or in other processes and then we have to process all of them at backend.

We can do this using HTML5 attribute "multiple" in this way :

1. First include the attribute enctype='multipart/form-data' in form tag otherwise you can't access/read the files at server end on submit.

<g:form enctype='multipart/form-data' method='POST' action='saveUserProfile' controller='user'> 
...    
</g:form>

2. Use HTML5 multiple attribute for file inpute tag :

<g:form enctype='multipart/form-data' method='POST' action='saveUserProfile' controller='user'>
    <input type='file' name='userFiles[]' multiple />    //Using this we have upload multiple files in one go. 
    <button type='submit'>Submit</button>
</g:form>

3. On server end we will read and process the files one by one in such a manner :

request.getFiles("userFiles[]").each { file ->
 log.debug(file.originalFilename) // persistence logic or logic as per requirement.
}

Simple and easy approach to upload multiple files at a time.

Thanks

Shiv Kumar

About Author

Author Image
Shiv Kumar

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..