mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-29 12:41:03 +00:00
Refactor Mongoose documentation
PUBLISHED_FROM=e9a4e5c7b4a1d03b93a2a79e29de19e60e919929
This commit is contained in:
1 parent
4120f953c7
commit
12437fd7fe
87 files changed
+977
-332
No files matched your search
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: Handling file uploads
|
||||
---
|
||||
|
||||
In order to handle file uploads, use the following HTML snippet:
|
||||
|
||||
```HTML
|
||||
<form method="POST" action="/upload" enctype="multipart/form-data">
|
||||
<input type="file" name="file">
|
||||
<input type="submit" value="Upload">
|
||||
</form>
|
||||
```
|
||||
|
||||
Uploaded files will be sent to the `/upload` endpoint via the `POST` request.
|
||||
HTTP body will contain multipart-encoded buffer with the file contents.
|
||||
|
||||
To save the uploaded file, use this code snippet:
|
||||
|
||||
```c
|
||||
struct mg_str cb(struct mg_connection *c, struct mg_str file_name) {
|
||||
// Return the same filename. Do not actually do this except in test!
|
||||
// fname is user-controlled and needs to be sanitized.
|
||||
return file_name;
|
||||
}
|
||||
|
||||
void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
switch (ev) {
|
||||
...
|
||||
case MG_EV_HTTP_PART_BEGIN:
|
||||
case MG_EV_HTTP_PART_DATA:
|
||||
case MG_EV_HTTP_PART_END:
|
||||
mg_file_upload_handler(c, ev, ev_data, cb);
|
||||
break;
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in new issue
Block a user