Skip to content

Commit 4cba4c7

Browse files
save file
1 parent 69648e1 commit 4cba4c7

File tree

1 file changed

+73
-13
lines changed
  • blog/25-07-31/fetch-download-stream/ex

1 file changed

+73
-13
lines changed

blog/25-07-31/fetch-download-stream/ex/ex.html

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
11

22
<style>
3+
#root
4+
{font-family:arial;--unknown:}
5+
.label
6+
{display:inline-block;width:150px;text-align:right;margin:5px 10px}
7+
#progress
8+
{border:1px solid gray;padding:2px;height:20px;margin:20px}
9+
#bar
10+
{background:lightgreen;width:0;height:100%}
11+
12+
#bar.unknown
13+
{opacity:0.5;background-size:20px 20px;background-image:repeating-linear-gradient(-45deg,#f7b733 0,#f7b733 5px,yellow 0,yellow 50%);
14+
animation: stripes-scroll 1s linear infinite;}
15+
16+
@keyframes stripes-scroll {
17+
from {background-position: 0 0;}
18+
to {background-position: 20px 0;}
19+
}
20+
321
</style>
422

523
<div id=root>
624

725
<div>
8-
encoding
26+
<span class=label>
27+
encoding
28+
</span>
929
<span id=encoding></span>
1030
</div>
1131
<div>
12-
content-length
32+
<span class=label>
33+
content-length
34+
</span>
1335
<span id=content-length></span>
1436
</div>
37+
<div>
38+
<span class=label>
39+
content-type
40+
</span>
41+
<span id=content-type></span>
42+
</div>
1543
<div id=progress>
1644
<div id=bar></div>
1745
</div>
18-
46+
<div id=result>
47+
<div>
48+
<div>
49+
<span class=label>name</span>
50+
<span id=name></span>
51+
</div>
52+
<div>
53+
<span class=label>size</span>
54+
<span id=size></span>
55+
</div>
56+
</div>
1957
</div>
2058

2159

2260
<script>
2361

2462
(async()=>{
25-
console.clear();
63+
console.clear();
2664

27-
28-
var url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip'
65+
var $ = (root,sel)=>(!sel && (sel=root,root=document),root.querySelector(sel));
2966

3067

31-
//var url = 'https://cdn.jsdelivr.net/gh/javascript-2020/libs/js/io/tiny-unzip/tiny-unzip.m.js';
68+
//varr url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip'
69+
var url = 'https://cdn.jsdelivr.net/gh/javascript-2020/libs/js/io/tiny-unzip/tiny-unzip.m.js';
70+
71+
var i = url.lastIndexOf('/')+1
72+
var fn = url.slice(i);
3273

3374
var headers = {};
3475
var method = 'get';
@@ -38,22 +79,36 @@
3879

3980
var encoding = res.headers.get('content-encoding')?.toLowerCase();
4081
if(encoding && encoding!=='identity'){
41-
console.log('unable to reliably computer content-length');
82+
encoding = false;
83+
$('#bar').className = 'unknown';
84+
$('#bar').style.width = '100%';
85+
console.log('unable to reliably computer content-length');
86+
}else{
87+
encoding = true;
4288
}
89+
90+
var type = res.headers.get('content-type');
91+
$('#content-type').textContent = type;
92+
4393
var len = res.headers.get('content-length');
44-
console.log(len);
94+
console.log(len);
4595
var total = 0;
4696
var reader = res.body.getReader();
4797
var chunks = [];
4898
while(true){
4999

50100
const {value,done} = await reader.read();
51101
if(done)break;
52-
total += value.length;
53-
console.log('chunk',datatype(value),value.byteLength,total,(total/len*100).toFixed(1)+' %');
102+
total += value.length;
54103
chunks.push(value);
55-
104+
105+
if(encoding){
106+
var w = (total/len*100).toFixed(1)+'%';
107+
$('#bar').style.width = w;
108+
console.log('chunk',datatype(value),value.byteLength,total,w);
109+
}
56110
}//while
111+
57112
var total = chunks.reduce((acc,chunk)=>acc+chunk.length,0);
58113
var buf = new Uint8Array(total);
59114
var offset = 0;
@@ -63,10 +118,15 @@
63118
offset += chunk.length;
64119

65120
}//for
121+
66122
var blob = new Blob([buf],{type:'application/octet-stream'});
67-
console.log(blob);
123+
blob.name = fn;
124+
console.log(blob);
68125

126+
$('#result #name').textContent = fn;
127+
$('#result #size').textContent = blob.size;
69128

129+
$('#bar').classList.remove('unknown');
70130

71131
function datatype(v){return Object.prototype.toString.call(v).slice(8,-1).toLowerCase()}
72132

0 commit comments

Comments
 (0)