diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-27 18:38:35 +0200 |
| commit | d8c4d84dc75300c6d4d8b0adceafa33741960b92 (patch) | |
| tree | 00e2dfcc5c836d62fccff76c862e6ec3b0a74db8 /libs/cpp-httplib/example/upload.cc | |
| parent | fa088bb60692ba02d30d39affa9a31d9e2b688e2 (diff) | |
added http lib, working on AI invoice importing
Diffstat (limited to 'libs/cpp-httplib/example/upload.cc')
| -rw-r--r-- | libs/cpp-httplib/example/upload.cc | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/libs/cpp-httplib/example/upload.cc b/libs/cpp-httplib/example/upload.cc new file mode 100644 index 0000000..f7a822b --- /dev/null +++ b/libs/cpp-httplib/example/upload.cc @@ -0,0 +1,61 @@ +// +// upload.cc +// +// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// MIT License +// + +#include <fstream> +#include <httplib.h> +#include <iostream> +using namespace httplib; +using namespace std; + +const char *html = R"( +<form id="formElem"> + <input type="file" name="image_file" accept="image/*"> + <input type="file" name="text_file" accept="text/*"> + <input type="submit"> +</form> +<script> + formElem.onsubmit = async (e) => { + e.preventDefault(); + let res = await fetch('/post', { + method: 'POST', + body: new FormData(formElem) + }); + console.log(await res.text()); + }; +</script> +)"; + +int main(void) { + Server svr; + + svr.Get("/", [](const Request & /*req*/, Response &res) { + res.set_content(html, "text/html"); + }); + + svr.Post("/post", [](const Request &req, Response &res) { + const auto &image_file = req.form.get_file("image_file"); + const auto &text_file = req.form.get_file("text_file"); + + cout << "image file length: " << image_file.content.length() << endl + << "image file name: " << image_file.filename << endl + << "text file length: " << text_file.content.length() << endl + << "text file name: " << text_file.filename << endl; + + { + ofstream ofs(image_file.filename, ios::binary); + ofs << image_file.content; + } + { + ofstream ofs(text_file.filename); + ofs << text_file.content; + } + + res.set_content("done", "text/plain"); + }); + + svr.listen("localhost", 1234); +} |
