简体   繁体   中英

unable to Generate a ZIP file on the fly and send it to the user’s browser for download

I have URLs of Audio Clips. I want to download all those clips from their's URLs and want to put all these clips in single zip file. If I specify the hard coded path in code, I am able to successfully download that on specified location in zip form with all clips. But, I want to generate pop-up window prompting user to specify the path to download that zip file. Here is my code:

  ByteArrayOutputStream baos = new ByteArrayOutputStream()
  ZipOutputStream zipFile = new ZipOutputStream(baos)

  for (int i = 0; i < clipsCount; i++) {            

        String fileName = getSessionId() + ".mp4";

        try {

            //Opening Connection to the Downloading Link
            URLConnection conn = new URL(sessionToDownload.getConvertedLinkURL()).openConnection();
            conn.setRequestProperty("Authorization", "Basic " + encodedString);

            //Adding File in the Zipped Resource
            def file1Entry = new ZipEntry(fileName);
            zipFile.putNextEntry(file1Entry);

            //Getting Required File
            zipFile << conn.getInputStream();

            //Closing Current Zip Entry
            zipFile.closeEntry();

            println "Session to Download: " + listofId.getAt(i);
        } catch (Exception ex) {
            println "Exception while Downloading Session: "+ex.getMessage();
        }
    }

    zipFile.close()
    response.setHeader("Content-disposition", "attachment; filename=\"/download.zip\" ")
    response.setContentType("application/zip");
    response.outputStream << baos
    response.outputStream.flush()
    webRequest.renderView = false;

So, here in Loop it iterates for each clip, download it and make it part of zip file.

Kindly, guide me if I am doing anything wrong or I can achieve this by some other way. Thanks for you time, consideration and guidance.

Your action should work actually. The browser Save as.. dialog automatically opens, if it sees "Content-disposition" -header with "attachment; filename=aaa.zip" as a value in the response.

Try removing the quotes:

response.setHeader 'Content-disposition', 'attachment; filename=download.zip'

I have Investigated this Issue actually it was produced due to issue described here: Sending Multiple Selected Items from Controller to View

There was one more issue that was not allowing it to appear at browser end. Here is what worked for me:

    //Setting Response Required Parameters
    response.setContentType('APPLICATION/OCTET-STREAM')
    response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"')

     InputStream contentStream = null;
    ZipOutputStream zip = new ZipOutputStream(response.outputStream);

    for (int i = 0; i < listofId.length; i++) {

        def sessionToDownload = Sessions.get(listofId.getAt(i));

        if (sessionsToDownload != null) {

            String fileName = sessionToDownload.getSessionId() + ".mp4";

            try {

                //Opening Connection to the Downloading Link
                URLConnection conn = new URL(sessionToDownload.getConvertedLinkURL()).openConnection();
                conn.setRequestProperty("Authorization", "Basic " + encodedString);

                //Getting file contents
                contentStream = conn.getInputStream();

                //Adding File in the Zipped Resource
                def file1Entry = new ZipEntry(fileName);
                zip.putNextEntry(file1Entry);
                zip.write(IOUtils.toByteArray(contentStream));

                println "Session to Download: " + fileName;
            } catch (Exception ex) {
                println "Exception while Downloading Session: " + ex.getMessage();
            }
        }
    }

    zip.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM