Suppose that you have string array and want to download each of them in separate file.
String[] str = new String[5]; str[0] = "File1 Data"; str[1] = "File2 Data"; str[2] = "File3 Data"; str[3] = "File4 Data"; str[4] = "File5 Data";
If You want to download multiple number of files, you must add them to a zip file and then download it.
This code will create a zip file.
ByteArrayOutputStream baos = new ByteArrayOutputStream() ZipOutputStream zipFile = new ZipOutputStream(baos)
Now run the loop for the files you want to add in the zip file, in grails you can use code below
for(int i =0;i<5;i++) { zipFile.putNextEntry(new ZipEntry("report"+i+".txt")); zipFile << str[i]; zipFile.closeEntry(); }
in case of java you can use write() of ZipOutputStream
Now close the ZipOutputStream object and prepare the response header
zipFile.finish(); response.setContentType("APPLICATION/DOWNLOAD"); response.setHeader("Content-disposition", "attachment; filename=\"ZippedFile.zip\"")
To send the zip file as response in grails
response.outputStream << baos.toByteArray()
for sending response in java
response.getOutputStream().write(baos.toByteArray());
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ashish Sharma
Ashish is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Ashish likes PC games and works out at Gym in his free time.