mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-30 12:50:58 +00:00
Change mg_http_upload API
This commit is contained in:
1 parent
56412193e7
commit
99f0688377
10 files changed
+135
-56
No files matched your search
@@ -10,7 +10,14 @@ static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_http_match_uri(hm, "/upload")) {
|
||||
mg_http_upload(c, hm, &mg_fs_posix, "/tmp");
|
||||
char path[80], name[64];
|
||||
mg_http_get_var(&hm->query, "name", name, sizeof(name));
|
||||
if (name[0] == '\0') {
|
||||
mg_http_reply(c, 400, "", "%s", "name required");
|
||||
} else {
|
||||
mg_snprintf(path, sizeof(path), "/tmp/%s", name);
|
||||
mg_http_upload(c, hm, &mg_fs_posix, mg_remove_double_dots(path), 99999);
|
||||
}
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
|
||||
@@ -18,12 +18,19 @@ var sendFileData = function(name, data, chunkSize) {
|
||||
var chunk = data.subarray(offset, offset + chunkSize) || '';
|
||||
var opts = {method: 'POST', body: chunk};
|
||||
var url = '/upload?offset=' + offset + '&name=' + encodeURIComponent(name);
|
||||
var ok;
|
||||
setStatus(
|
||||
'sending bytes ' + offset + '..' + (offset + chunk.length) + ' of ' +
|
||||
data.length);
|
||||
fetch(url, opts).then(function(res) {
|
||||
if (chunk.length > 0) sendChunk(offset + chunk.length);
|
||||
});
|
||||
'Upoading ' + name + ', bytes ' + offset + '..' +
|
||||
(offset + chunk.length) + ' of ' + data.length);
|
||||
fetch(url, opts)
|
||||
.then(function(res) {
|
||||
if (res.ok && chunk.length > 0) sendChunk(offset + chunk.length);
|
||||
ok = res.ok;
|
||||
return res.text();
|
||||
})
|
||||
.then(function(text) {
|
||||
if (!ok) setStatus('Error: ' + text);
|
||||
});
|
||||
};
|
||||
sendChunk(0);
|
||||
};
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
in small chunks.
|
||||
<br/><br/>
|
||||
In this example, the JavaScript code on this page sends the chosen
|
||||
file in 4K chunks using the <code>/upload</code> endpoint.
|
||||
file in 2Kb chunks using the <code>/upload</code> endpoint.
|
||||
The uploaded file is stored in the <code>/tmp</code> directory by
|
||||
the helper API function <code>mg_http_upload()</code>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user