0

I am using JSZip to generate a zip containing multiple files. The data is coming from the server in JSON format. I am only getting 2 files inside the for loop instead of multiple files. The size of the array of JSON is more than 2 so I expect multiple files not just 2.

var zip = new JSZip();
var root = data.list.dataList;
var length = root.length;
var i;
for(i = 0; i < length; i++){                                                    
    var aFileName = "a_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file                         
    var aContent = root[i].content; //content of file                               

    var bFileName = "b_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file         
    var bContent = root[i].content; //content of file   

    //put files onto a folder
    zip.folder("Folder Name").file(aFileName, aContent).file(bFileName, bContent); 
}
//generate the zip with all files
var content = zip.generate();
location.href="data:application/zip;base64," + content;     

can someone please suggest an alternative since this code only take the content of the last iteration. This is why I can only get 2 files instead of multiple files.

onelazyguy
  • 41
  • 2
  • 9

2 Answers2

1

Try this:

for(i = 0; i < length; i++){
        var aFileName = "a_"+ root[i].type + "_" + root[i].orderNumber + "_" + i + ".xml";
        var aContent = root[i].content; //content of file

        var bFileName = "b_"+ root[i].type + "_" + root[i].orderNumber + "_" + i + ".xml";
        var bContent = root[i].content; //content of file

        //put files onto a folder
        zip.folder("Folder Name").file(aFileName, aContent).file(bFileName, bContent);
    }

I try similar as follows and the generated folder contains 6 files.

var JSZip = require("jszip");
var fs = require("fs");
var zip = new JSZip();
var root = [{type:"type1",orderNumber:"1",content:"content1"},
    {type:"type2",orderNumber:"2",content:"content2"},
    {type:"type3",orderNumber:"3",content:"content3"}];
var length = root.length;
var i;
for(i = 0; i < length; i++){
    var aFileName = "a_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file
    var aContent = root[i].content; //content of file

    var bFileName = "b_"+ root[i].type + "_" + root[i].orderNumber + ".xml";//just the name of the file
    var bContent = root[i].content; //content of file

    //put files onto a folder
    zip.folder("json_folder").file(aFileName, aContent).file(bFileName, bContent);
}
//generate the zip with all files
var content = zip.generate({type:"nodebuffer"});

fs.writeFile("json_folder.zip", content, function(err) {
    if (err) throw err;
});

So I think maybe your problem occurs because your type and orderNumber is the same for each item so that you overwrite your file content again and again.

JasmineOT
  • 1,978
  • 2
  • 20
  • 30
-1

Try out plugin: http://gildas-lormeau.github.io/zip.js/ Else try to wrap your loop in

try
{

}
catch (ex)
{
console.log(ex.message);
}

And have a look if he throws some errors

graphefruit
  • 364
  • 4
  • 15
  • 1
    He asked for an alternative, so I provided an alternative framework without seeing the data he gets I cant say why just 2 files are added – graphefruit Sep 25 '14 at 04:38
  • Update, cant update my old comment: I used the posted framework with success. Also maybe the loop is crashing cause a property not set, thats why the try catch is for. – graphefruit Sep 25 '14 at 04:45
  • Can someone give me an working example similar to this issue? I am still facing this and out of idea of what to do. – onelazyguy Sep 25 '14 at 14:39
  • Have you already tried the try-catch, also list the "data.list.datalist" object structure – graphefruit Sep 25 '14 at 15:24
  • I have exhausted everything I could think of plus the try/catch block...firebug shows the JSON content from each iteration perfectly fine. I found a similar question asked here: http://stackoverflow.com/questions/17986432/how-to-zip-files-using-jszip-library but the suggested solution is not using the loop..I am not sure how to implement..could anyone shred some light? – onelazyguy Sep 25 '14 at 16:14
  • issue has been fixed..thanks graphefruit for your input – onelazyguy Sep 25 '14 at 17:46
  • Would be interested in hearing the solution;) – graphefruit Sep 25 '14 at 18:49