3

Hello Developers, I'm trying to download the list of files getting form XMLHttpRequest() request method and store it in the array of files. I wanted to zip all the files in an array using javascript without using any 3rd party library. I have tried to achieve this by URL.createObjectURL(url) and URL.revokeObjectURL(url) method but the file I'm getting is corrupted.

I'm Sharing my code snippet please help me out

 const URLS = [
            'https://vr.josh.earth/assets/2dimages/saturnv.jpg',
            'https://vr.josh.earth/assets/360images/hotel_small.jpg',
            'https://vr.josh.earth/assets/360images/redwoods.jpg'
        ];
        $(document).ready(function () {
            debugger
            $("#downloadAll").click(function () {
                var blob = new Array();
                var files = new Array();
                URLS.forEach(function (url, i) {
                    getRawData(url, function (err, data) {
                        debugger
                        var mydata = data;
                        // mydata = btoa(encodeURIComponent(data));
                        // var blobData = b64toBlob(mydata , 'image/jpg');
                        var blobData = new Blob([mydata], { type: 'image/jpg' });
                        blob.push(blobData);
                        var filename = "testFiles" + i + ".jpg";
                        var file = blobToFile(blobData, filename);
                        files.push(file);

                        debugger
                        if (files.length == URLS.length) {
                            // saveData(blob, "fileName.zip");
                            var AllBlobData = new Blob([blob], { type: 'application/zip' });
                            saveData(AllBlobData, "Test.zip");
                            // saveFile("DownloadFiles.zip", "application/zip", files)

                        }
                    });
                });
            });

        });

//Retriving record using XMLHttpRequest() method.

  function getRawData(urlPath, callback, progress) {
            var request = new XMLHttpRequest();
            request.open("GET", urlPath, true);
            request.setRequestHeader('Accept', '');
            if ('responseType' in request)
                request.responseType = "arraybuffer"
            if (request.overrideMimeType)
                request.overrideMimeType('text/plain; charset=x-user-defined');
            request.send();
            var file, err;
            request.onreadystatechange = function () {
                if (this.readyState === 4) {
                    request.onreadystatechange = null;
                    if (this.status === 200) {
                        try {
                            debugger
                            var file = request.response || request.responseText;
                        } catch (error) {
                            throw error;
                        }
                        callback(err, file);

                    } else {
                        debugger
                        callback(new Error("Ajax Error!!"))
                    }
                } else {
                    debugger
                }
            }
        }

//For Saving the file into zip

  var saveData = (function () {
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.style = "display: none";
            return function (data, fileName) {
                // var AllBlobs = new Blob([data], { type: "" });//application/zip  //octet/stream
                // var url = window.URL.createObjectURL(AllBlobs);
                var url = window.URL.createObjectURL(data);
                a.href = url;
                a.download = fileName;
                a.click();
                window.URL.revokeObjectURL(url);
            };
        }());

//Downloaded Zip File

enter image description here

  • Without third-parties you need to implement zip archiver on your own. Why do you need it without a library? Also, why did you decide that `createObjectURL` return a zip? – Eugene Obrezkov May 08 '20 at 11:37
  • My client doesn't want to add any 3rd party library that's why, I try to implement this by finding this solution from this link (https://stackoverflow.com/questions/31127849/how-to-save-binary-data-of-zip-file-in-javascript) – Amarjeet Singh May 08 '20 at 15:57

0 Answers0