用fetch
来异步接收
获取到result.value
为uint8array
bytes
数组来处理
code
let url = 'http://com.a:8080/flv/stream';
// var url = 'LargeFile.txt';
let progress = 0;
let contentLength = 0;
fetch(url).then(function(response) {
// get the size of the request via the headers of the response
contentLength = response.headers.get('Content-Length');
let pump = function(reader) {
return reader.read().then(function(result) {
// if we're done reading the stream, return
if (result.done) {
return;
}
// retrieve the multi-byte chunk of data
// var chunk = result.value;
// var text = '';
console.log(result.value);
// since the chunk can be multiple bytes, iterate through
// each byte while skipping the byte order mark
// (assuming UTF-8 with single-byte chars)
// for (var i = 3; i < chunk.byteLength; i++) {
// text += String.fromCharCode(chunk[i]);
// }
// append the contents to the page
// document.getElementById('content').innerHTML += text;
// console.log(text);
// report our current progress
// progress += chunk.byteLength;
// console.log(((progress / contentLength) * 100) + '%');
// go to next chunk via recursion
return pump(reader);
});
}
// start reading the response stream
return pump(response.body.getReader());
})
.catch(function(error) {
console.log(error);
});
网友评论