Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  93] [ 1]  / answers: 1 / hits: 40709  / 11 Years ago, thu, august 1, 2013, 12:00:00

Am working on an offline application using HTML5 and jquery for mobile. i want to back up files from the local storage using jszip. below is a code snippet of what i have done...



  if (localStorageKeys.length > 0) {
for (var i = 0; i < localStorageKeys.length; i++) {
var key = localStorageKeys[i];
if (key.search(_instrumentId) != -1) {
var data = localStorage.getItem(localStorageKeys[i])
var zip = new JSZip();
zip.file(localStorageKeys[i] + .txt, data);
var datafile = document.getElementById('backupData');
datafile.download = DataFiles.zip;
datafile.href = window.URL.createObjectURL(zip.generate({ type: blob }));
}
else {

}


}

}


in the code above am looping through the localstorage content and saving ezch file in a text format. the challenge that am facing is how to create several text files inside DataFiles.zip as currently am only able to create one text file inside the zipped folder. Am new to javascript so bare with any ambiguity in my question.
thanks in advance.


More From » jquery

 Answers
0

Just keep calling zip.file().



Look at the example from their documentation page (comments mine):



var zip = new JSZip();

// Add a text file with the contents Hello Worldn
zip.file(Hello.txt, Hello Worldn);

// Add a another text file with the contents Goodbye, cruel worldn
zip.file(Goodbye.txt, Goodbye, cruel worldn);

// Add a folder named images
var img = zip.folder(images);

// Add a file named smile.gif to that folder, from some Base64 data
img.file(smile.gif, imgData, {base64: true});

zip.generateAsync({type:base64}).then(function (content) {
location.href=data:application/zip;base64, + content;
});


The important thing is to understand the code you've written - learn what each line does. If you do this, you'd realize that you just need to call zip.file() again to add another file.


[#76606] Wednesday, July 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cindyanyssam

Total Points: 483
Total Questions: 94
Total Answers: 100

Location: Barbados
Member since Sat, May 28, 2022
2 Years ago
;