forked from Stuk/jszip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr-download-zip-file.html
77 lines (69 loc) · 1.96 KB
/
xhr-download-zip-file.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSZip example : download the generated zip file</title>
<link media="screen" href="style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="../jszip.js"></script>
<script type="text/javascript">
window.onload = function () {
var zip = new JSZip();
function read(cb){
var xhr = new XMLHttpRequest();
xhr.open('GET', './kitten.jpg', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if(this.status == 200){
var blob = new Blob([this.response], {type: "image/png"});
reader = new FileReader();
reader.onload = function (event){
if(event.loaded == event.total)
cb(event.target.result);
}
reader.readAsBinaryString(blob);
}
}
xhr.send(null);
}
function readOld(cb) {
var xhr = new XMLHttpRequest();
xhr.open('GET', './kitten.jpg', true);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.onload = function(e) {
if(this.status == 200){
var data = xhr.responseText;
var bytes = '';
for(var i = 0; i < data.length; i++){
code = data.charCodeAt(i) & 0xff
bytes += String.fromCharCode(code)
}
cb(bytes);
}
}
xhr.send(null);
}
//readOld(function(data){
read(function(data){
console.log(data);
f = zip.folder('inhere')
f.file('kitten.jpg', data, {binary:true})
});
// Blob
var blobLink = document.getElementById('blob');
try {
blobLink.download = "kitten.zip";
blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
} catch(e) {
blobLink.innerHTML += " (not supported on this browser)";
}
};
</script>
</head>
<body>
<h1><a href="../">JSZip</a> example : download the generated zip file</h1>
<h2>The Blob URL</h2>
<div>
<a href="#" id="blob">click to download</a>
</div>
</body>
</html>