Home » Java » Unable to downloading Zip file from Ajax byte reponse(converting it to Blob) in js

Unable to downloading Zip file from Ajax byte reponse(converting it to Blob) in js

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am having a scenario where i have to create/download a zip file that is created using multiple files from url’s in a jsp(No servlet imlementation) which is called using only ajax call as i need to implement progress bar (not accurate just for a view) in js file .

I have tried creating a zipoutputstream in jsp and it is perfectly going well using

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ZipOutputStream  zos = new ZipOutputStream( bos );

finally sending the data in byte stream

response.getOutputStream().write(**bos.toByteArray()**);                   
                    response.flushBuffer();

In Js file i have tried the follwing code

var $download_status = $.ajax({

                    "url" :  jsp url where zip is created
                    cache: false,
                    dataType: "html",
                    contentType: "application/zip",                    

                    success : function (data) {
                       console.log(data.length);
                        //the data is having the exact size as the zos 
                        var blob = new Blob([data]);
                       // the blob file is getting 2X size 
                        var link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = seiesName+".zip";
                        link.click();
                    },
                }); 

When i downloaded the file i am getting corrupted file whose size is almost 2x to the original file .

Please any one help me with this …

Thanks in advance…

Regards,
Vamc.

Answers: