Last active
June 13, 2017 07:31
-
-
Save OndrejIT/3af6cef991a5adb3606921f08df46a6a to your computer and use it in GitHub Desktop.
onlinezipi.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function ($) { | |
"use strict"; | |
/** | |
* Reset the message. | |
*/ | |
function resetMessage() { | |
$("#result") | |
.removeClass() | |
.text(""); | |
} | |
/** | |
* show a successful message. | |
* @param {Number} percent the text to show. | |
*/ | |
function showMessage(percent) { | |
resetMessage(); | |
$("#result") | |
.addClass("alert alert-success") | |
.text("Progress: " + percent + " %"); | |
} | |
/** | |
* show an error message. | |
* @param {String} text the text to show. | |
*/ | |
function showError(text) { | |
resetMessage(); | |
$("#result") | |
.addClass("alert alert-danger") | |
.text(text); | |
} | |
/** | |
* Update the progress bar. | |
* @param {Number} percent the current percent | |
*/ | |
function updatePercent(percent) { | |
$("#progress_bar").removeClass("hide") | |
.find(".progress-bar") | |
.attr("aria-valuenow", percent) | |
.css({ | |
width: percent + "%" | |
}); | |
} | |
if (!JSZip.support.blob) { | |
showError("This demo works only with a recent browser !"); | |
return; | |
} | |
$("#download_form").on("submit", function (e) { | |
e.preventDefault(); | |
resetMessage(); | |
var zip = new JSZip(); | |
var count = 0; | |
var urls = $(":checked"); | |
showMessage(count); | |
updatePercent(count); | |
processZip(); | |
function processZip() { | |
var url = urls.eq(count).data("url"); | |
var filename = url.replace(/.*\//g, ""); | |
JSZipUtils.getBinaryContent(url, function (err, data) { | |
if (err) { | |
throw err; | |
} | |
zip.file(filename, data, {binary: true}); | |
count++; | |
var percent = count * (100 / urls.length); | |
showMessage(percent); | |
updatePercent(percent); | |
if (count === urls.length) { | |
zip.generateAsync({type: "blob"}).then(function (content) { | |
saveAs(content, "example.zip"); | |
}); | |
} else { | |
processZip(); | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment