Die Funktion add() habe ich in der Doku nicht gefunden, dafür jedoch die Funktion file(). Wenn ich dabei die Option binary auf true setze und dieses:
http://www.henryalgus.com/reading-binary…ng-jquery-ajax/
anwende, komme ich zu diesem Code:
Code
<script src="js/jszip.min.js"></script>
<script type="text/javascript" src="//stuk.github.io/jszip-utils/dist/jszip-utils.js"></script>
<script type="text/javascript" src="js/fileSaver.js"></script>
</head>
<body>
<button id="blob" class="btn btn-primary">click to download</button>
<script>
jQuery("#blob").on("click", function () {
//var promise = $.get("test.pdf");
//zip.file("test.pdf", promise, {binary: true});
var xhr = new XMLHttpRequest();
xhr.open('GET', 'test.pdf', true);
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (this.status == 200) {
// get binary data as a response
var blob = this.response;
var zip = new JSZip();
zip.file("test.pdf", blob, { binary: true });
zip.generateAsync({ type: "blob" }).then(function (blob) { // 1) generate the zip file
saveAs(blob, "test.zip"); // 2) trigger the download
}, function (err) {
jQuery("#blob").text(err);
});
}
};
xhr.send();
});
</script>
Alles anzeigen