mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-05 10:58:59 +08:00
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
// Copyright (c) 2020 Cesanta Software Limited
|
|
// All rights reserved
|
|
|
|
// Helper function to display upload status
|
|
var setStatus = function(text) {
|
|
document.getElementById('el3').innerText = text;
|
|
};
|
|
|
|
// When user clicks on a button, trigger file selection dialog
|
|
var button = document.getElementById('el2');
|
|
button.onclick = function(ev) {
|
|
input.click();
|
|
};
|
|
|
|
// Send a large blob of data chunk by chunk
|
|
var sendFileData = function(name, data, chunkSize) {
|
|
var sendChunk = function(offset) {
|
|
var chunk = data.subarray(offset, offset + chunkSize) || '';
|
|
var opts = {method: 'POST', body: chunk};
|
|
var url = '/upload?offset=' + offset + '&name=' + encodeURIComponent(name);
|
|
setStatus(
|
|
'sending bytes ' + offset + '..' + (offset + chunk.length) + ' of ' +
|
|
data.length);
|
|
fetch(url, opts).then(function(res) {
|
|
if (chunk.length > 0) sendChunk(offset + chunk.length);
|
|
});
|
|
};
|
|
sendChunk(0);
|
|
};
|
|
|
|
// If user selected a file, read it into memory and trigger sendFileData()
|
|
var input = document.getElementById('el1');
|
|
input.onchange = function(ev) {
|
|
if (!ev.target.files[0]) return;
|
|
var f = ev.target.files[0], r = new FileReader();
|
|
r.readAsArrayBuffer(f);
|
|
r.onload = function() {
|
|
ev.target.value = '';
|
|
sendFileData(f.name, new Uint8Array(r.result), 4096);
|
|
};
|
|
};
|