Switch to fetch to retrieve files

This commit is contained in:
Anthony
2021-01-28 10:17:30 +07:00
committed by GitHub
parent 649d10d8f9
commit e0143c9f59
+20 -34
View File
@@ -74,44 +74,30 @@ self.addEventListener("message", function(e)
function isNumeric(n)
{
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
function send_error(s)
{
postMessage({msg_type:MSG_ERROR, data:s});
}
function download_from_local(filename)
{
var xhr = new XMLHttpRequest();
if (get_progress)
{
xhr.onprogress =
function(e)
{
postMessage({msg_type:MSG_LOAD_IN_PROGRESS, id:model_id, loaded:e.loaded, total:e.total});
}
}
xhr.onreadystatechange =
function(e)
{
if (xhr.readyState == 4)
{
//console.log('status: '+xhr.status);
if (xhr.status==200)
{
//console.log('done');
after_file_load(xhr.response);
}
}
}
xhr.open("GET", filename, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.responseType = "arraybuffer";
async function download_from_local(filename) {
const response = await fetch(filename)
const reader = response.body.getReader()
const total = Number(response.headers.get('content-length'))
const chunksAll = new Uint8Array(total)
let position = 0
while (true) {
const { done, value } = await reader.read()
if (done) break
if (!value) continue
chunksAll.set(value, position)
position += value.length
if (get_progress) {
postMessage({ msg_type: MSG_LOAD_IN_PROGRESS, id: model_id, loaded: position, total: total })
}
}
after_file_load(chunksAll.buffer)
}
function after_file_load(s)
@@ -190,4 +176,4 @@ function read_file(f)
};
reader.readAsArrayBuffer(f);
reader.readAsArrayBuffer(f);
}