mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Content receiver support for multipart content (Fix #241)
This commit is contained in:
parent
5e37e38398
commit
b69c0a1dcb
3 changed files with 459 additions and 191 deletions
28
README.md
28
README.md
|
@ -90,7 +90,7 @@ svr.Post("/multipart", [&](const auto& req, auto& res) {
|
|||
const auto& file = req.get_file_value("name1");
|
||||
// file.filename;
|
||||
// file.content_type;
|
||||
auto body = req.body.substr(file.offset, file.length);
|
||||
// file.content;
|
||||
});
|
||||
|
||||
```
|
||||
|
@ -118,12 +118,26 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
|
|||
```cpp
|
||||
svr.Post("/content_receiver",
|
||||
[&](const Request &req, Response &res, const ContentReader &content_reader) {
|
||||
std::string body;
|
||||
content_reader([&](const char *data, size_t data_length) {
|
||||
body.append(data, data_length);
|
||||
return true;
|
||||
});
|
||||
res.set_content(body, "text/plain");
|
||||
if (req.is_multipart_form_data()) {
|
||||
MultipartFiles files;
|
||||
content_reader(
|
||||
[&](const std::string &name, const char *data, size_t data_length) {
|
||||
auto &file = files.find(name)->second;
|
||||
file.content.append(data, data_length);
|
||||
return true;
|
||||
},
|
||||
[&](const std::string &name, const MultipartFile &file) {
|
||||
files.emplace(name, file);
|
||||
return true;
|
||||
});
|
||||
} else {
|
||||
std::string body;
|
||||
content_reader([&](const char *data, size_t data_length) {
|
||||
body.append(data, data_length);
|
||||
return true;
|
||||
});
|
||||
res.set_content(body, "text/plain");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue