MCPcopy Create free account
hub / github.com/cesanta/mongoose / sendFileData

Function sendFileData

tutorials/http/file-upload-multiple-posts/web_root/app.js:16–36  ·  view source on GitHub ↗
(name, data, chunkSize)

Source from the content-addressed store, hash-verified

14
15// Send a large blob of data chunk by chunk
16var sendFileData = function(name, data, chunkSize) {
17 var sendChunk = function(offset) {
18 var chunk = data.subarray(offset, offset + chunkSize) || '';
19 var opts = {method: 'POST', body: chunk};
20 var url = '/upload?offset=' + offset + '&file=' + encodeURIComponent(name);
21 var ok;
22 setStatus(
23 'Uploading ' + name + ', bytes ' + offset + '..' +
24 (offset + chunk.length) + ' of ' + data.length);
25 fetch(url, opts)
26 .then(function(res) {
27 if (res.ok && chunk.length > 0) sendChunk(offset + chunk.length);
28 ok = res.ok;
29 return res.text();
30 })
31 .then(function(text) {
32 if (!ok) setStatus('Error: ' + text);
33 });
34 };
35 sendChunk(0);
36};
37
38// If user selected a file, read it into memory and trigger sendFileData()
39var input = document.getElementById('el1');

Callers 1

app.jsFile · 0.70

Calls 1

sendChunkFunction · 0.70

Tested by

no test coverage detected