{"id":5324,"date":"2026-04-14T11:42:49","date_gmt":"2026-04-14T02:42:49","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5324"},"modified":"2026-04-14T14:28:50","modified_gmt":"2026-04-14T05:28:50","slug":"webserver-httplib-file-upload-macoos","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/04\/14\/webserver-httplib-file-upload-macoos\/","title":{"rendered":"[Webserver]httplib webserver+file upload(multipart,http)(macoOS)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb httplib \uc6f9\uc11c\ubc84\uc5d0\uc11c multipart \ud30c\uc77c \uc5c5\ub85c\ub4dc\ub97c \uad6c\ud604\ud558\ub294 \uc124\uba85\uc785\ub2c8\ub2e4.<br>This is a description of implementing multipart file uploads on an httplib web server.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ud504\ub85c\uc81d\ud2b8 \ub514\ub809\ud1a0\ub9ac\ub0b4\uc5d0 httplib.h\ud30c\uc77c\uc744 \ub2e4\uc6b4 \ubc1b\uc2b5\ub2c8\ub2e4.<br>Download the httplib.h file into the project directory.<\/p>\n\n\n\n<p>\u2714\ufe0f httplib.h(0.42.0)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -L https:\/\/raw.githubusercontent.com\/yhirose\/cpp-httplib\/master\/httplib.h -o httplib.h<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffbHttp server basic (upload_server.cpp)<\/p>\n\n\n\n<p>\u2714\ufe0f \uc544\ub798\ub294 cpp \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uc11c \ub85c\uceec \uc11c\ubc84\ub85c \ud30c\uc77c\uc744 \uc5c5\ub85c\ub4dc \ud558\ub294 \uc124\uba85\uc785\ub2c8\ub2e4.<br>The following is an explanation of uploading files from a C++ client to a local server.<\/p>\n\n\n\n<p>\u2714\ufe0f \uae43\ud5c8\ube0c\uc5d0\uc11c \ud30c\uc77c \ub2e4\uc6b4\ub85c\ub4dc\uc2dc basic \ub514\ub809\ud1a0\ub9ac \ub0b4\uc5d0 \uc788\ub294 \ud30c\uc77c \uc785\ub2c8\ub2e4.<br>This is the file located in the basic directory when downloading from GitHub.<\/p>\n\n\n\n<p>\u2714\ufe0f \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>&#8212; upload_server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"..\/httplib.h\"\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n\nint main() {\n    httplib::Server svr;\n\n    svr.Post(\"\/upload\", &#91;&amp;](const httplib::Request&amp; req, httplib::Response&amp; res,\n                            const httplib::ContentReader&amp; content_reader) {\n        if (!req.is_multipart_form_data()) {\n            res.status = 400;\n            res.set_content(\"Not multipart\/form-data\", \"text\/plain\");\n            return;\n        }\n\n        std::string filename;\n        std::ofstream ofs;\n\n        content_reader(\n            &#91;&amp;](const httplib::FormData&amp; item) {\n                if (item.name != \"file\") return true;\n\n                filename = httplib::sanitize_filename(item.filename);\n                if (filename.empty()) {\n                    res.status = 400;\n                    res.set_content(\"Invalid filename\", \"text\/plain\");\n                    return false;\n                }\n                \/\/ file upload directory\n                ofs.open(\"..\/uploads\/\" + filename, std::ios::binary);\n                if (!ofs) {\n                    res.status = 500;\n                    res.set_content(\"Failed to open file\", \"text\/plain\");\n                    return false;\n                }\n                return true;\n            },\n            &#91;&amp;](const char* data, size_t len) {\n                if (ofs) ofs.write(data, len);\n                return true;\n            }\n        );\n\n        if (ofs) ofs.close();\n\n        res.set_content(\"Upload OK\", \"text\/plain\");\n    });\n\n    std::cout &lt;&lt; \"Upload server listening on http:\/\/0.0.0.0:5080\\n\";\n    svr.listen(\"0.0.0.0\", 5080);\n}\n<\/code><\/pre>\n\n\n\n<p>&#8212; upload_client.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"..\/httplib.h\"\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n\nint main() {\n    httplib::Client cli(\"http:\/\/localhost:5080\");\n \n    \/\/ \uc5c5\ub85c\ub4dc \ud56d\ubaa9 \/ upload\n    httplib::UploadFormData item;\n    item.name = \"file\";\n    item.filename = \"test.txt\";\n    item.content_type = \"text\/plain\";\n\n    \/\/ \ud30c\uc77c \uc77d\uae30 \/ read file\n    std::ifstream ifs(\"test.txt\", std::ios::binary);\n    if (!ifs) {\n        std::cerr &lt;&lt; \"cannot open file\\n\";\n        return 1;\n    }\n    item.content.assign((std::istreambuf_iterator&lt;char&gt;(ifs)),\n                         std::istreambuf_iterator&lt;char&gt;());\n\n    httplib::UploadFormDataItems items;\n    items.push_back(item);\n\n    httplib::Headers headers; \n\n    if (auto res = cli.Post(\"\/upload\", headers, items)) {\n        std::cout &lt;&lt; \"status: \" &lt;&lt; res-&gt;status &lt;&lt; \"\\n\";\n        std::cout &lt;&lt; res-&gt;body &lt;&lt; \"\\n\";\n    } else {\n        std::cerr &lt;&lt; \"request failed\\n\";\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ upload_server.cpp -o upload_server -std=c++11<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc11c\ubc84 \uc2e4\ud589 \/ Run Server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/upload_server<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"95\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-1024x95.jpg\" alt=\"\" class=\"wp-image-5328\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-1024x95.jpg 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-300x28.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-768x71.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-400x37.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg-800x74.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_server-jpg.jpg 1032w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">\uc11c\ubc84\uc2e4\ud589 \/ Run Server<\/figcaption><\/figure>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ upload_client.cpp -o upload_client -std=c++11<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud074\ub77c\uc774\uc5b8\ud2b8 \uc2e4\ud589 \/ Run Client<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/upload_client<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"920\" height=\"124\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg.jpg\" alt=\"\" class=\"wp-image-5330\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg.jpg 920w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg-300x40.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg-768x104.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg-400x54.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/upload_client-jpg-800x108.jpg 800w\" sizes=\"auto, (max-width: 920px) 100vw, 920px\" \/><figcaption class=\"wp-element-caption\">\ud074\ub77c\uc774\uc5b8\ud2b8 \uc2e4\ud589 \/ Run Client<\/figcaption><\/figure>\n\n\n\n<p>\u2714\ufe0f \ud30c\uc77c \uc5c5\ub85c\ub4dc \uc0c1\ud0dc \ud655\uc778 \/ Check file upload status<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"110\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-1024x110.jpg\" alt=\"\" class=\"wp-image-5332\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-1024x110.jpg 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-300x32.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-768x82.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-400x43.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg-800x86.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/uploads-jpg.jpg 1234w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">\ud30c\uc77c \uc5c5\ub85c\ub4dc \uc0c1\ud0dc \ud655\uc778 \/ Check file upload status<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49\ud83c\udffbHttp server html (upload_server.cpp)<\/p>\n\n\n\n<p>\u2714\ufe0f \uc544\ub798\ub294 html\ud30c\uc77c\uc5d0\uc11c \ud30c\uc77c\uc744 \uc5c5\ub85c\ub4dc \ud558\ub294 \uc124\uba85\uc785\ub2c8\ub2e4.<br>Below is an explanation of how to upload a file in an HTML file.<\/p>\n\n\n\n<p>\u2714\ufe0f \uae43\ud5c8\ube0c\uc5d0\uc11c \ud30c\uc77c \ub2e4\uc6b4\ub85c\ub4dc\uc2dc uploads_html \ub514\ub809\ud1a0\ub9ac \ub0b4\uc5d0 \uc788\ub294 \ud30c\uc77c \uc785\ub2c8\ub2e4.<br>This is the file located in the uploads_html directory when downloading from GitHub.<\/p>\n\n\n\n<p>\u2714\ufe0f www \ub514\ub809\ud1a0\ub9ac\uac00 \uc5c6\uc73c\uba74 \ub514\ub809\ud1a0\ub9ac\ub97c \ub9cc\ub4e6\ub2c8\ub2e4.<br>If the www directory does not exist, create the directory.<\/p>\n\n\n\n<p>\u2714\ufe0f www \ub514\ub809\ud1a0\ub9ac \uc548\uc5d0 \uac04\ub2e8\ud55c index.html\ud30c\uc77c\uc744 \ub9cc\ub4e6\ub2c8\ub2e4.<br>Create a simple index.html file inside the www directory.<\/p>\n\n\n\n<p>\u2714\ufe0f index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!doctype html&gt;\n&lt;html lang=\"ko\"&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"utf-8\" \/&gt;\n        &lt;title&gt;File Upload&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;\ud30c\uc77c \uc5c5\ub85c\ub4dc\/File Upload&lt;\/h1&gt;\n        &lt;form action=\"\/upload\" method=\"post\" enctype=\"multipart\/form-data\"&gt;\n            &lt;input type=\"file\" name=\"file\" \/&gt;\n            &lt;button type=\"submit\"&gt;\uc5c5\ub85c\ub4dc\/Upload&lt;\/button&gt;\n        &lt;\/form&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0fupload_server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"..\/httplib.h\"\n#include &lt;fstream>\n#include &lt;iostream>\n#include &lt;sstream>\n\nint main() {\n    httplib::Server svr;\n\n    \/\/ html upload form\n    if (!svr.set_mount_point(\"\/\", \".\/www\")) {\n        std::cerr &lt;&lt; \"\uacbd\ub85c \ud3f4\ub354\ub97c \ucc3e\uc744 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4! \uacbd\ub85c\ub97c \ud655\uc778\ud558\uc138\uc694.\/The path folder cannot be found! Please check the path.\" &lt;&lt; std::endl;\n        return 1;\n    }\n\n    \/\/ \uc5c5\ub85c\ub4dc \ucc98\ub9ac \/ Upload processing\n    svr.Post(\"\/upload\", &#91;&amp;](const httplib::Request&amp; req, httplib::Response&amp; res,\n                            const httplib::ContentReader&amp; content_reader) {\n        if (!req.is_multipart_form_data()) {\n            res.status = 400;\n            res.set_content(\"Not multipart\/form-data\", \"text\/plain\");\n            return;\n        }\n\n        std::string filename;\n        std::ofstream ofs;\n\n        content_reader(\n            &#91;&amp;](const httplib::FormData&amp; item) {\n                if (item.name != \"file\") return true;\n\n                filename = httplib::sanitize_filename(item.filename);\n                if (filename.empty()) {\n                    res.status = 400;\n                    res.set_content(\"Invalid filename\", \"text\/plain\");\n                    return false;\n                }\n\n                ofs.open(\"..\/uploads\/\" + filename, std::ios::binary);\n                if (!ofs) {\n                    res.status = 500;\n                    res.set_content(\"Failed to open file\", \"text\/plain\");\n                    return false;\n                }\n                return true;\n            },\n            &#91;&amp;](const char* data, size_t len) {\n                if (ofs) ofs.write(data, len);\n                return true;\n            }\n        );\n\n        if (ofs) ofs.close();\n        res.set_content(\"Upload OK\", \"text\/plain\");\n    });\n\n    std::cout &lt;&lt; \"Server listening on http:\/\/0.0.0.0:5080\\n\";\n    svr.listen(\"0.0.0.0\", 5080);\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ upload_server.cpp -o upload_server -std=c++11<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc11c\ubc84\uc2e4\ud589 \/ Run Server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/upload_server<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ube0c\ub77c\uc6b0\uc800 \uc811\uc18d \/ Browser access<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"544\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg.jpg\" alt=\"\" class=\"wp-image-5335\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg.jpg 860w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg-300x190.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg-768x486.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg-400x253.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_index-jpg-800x506.jpg 800w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><figcaption class=\"wp-element-caption\">\/<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"316\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_upload_ok-jpg.png\" alt=\"\" class=\"wp-image-5336\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_upload_ok-jpg.png 676w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_upload_ok-jpg-300x140.png 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_upload_ok-jpg-400x187.png 400w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<p>upload ok<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"206\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg.jpg\" alt=\"\" class=\"wp-image-5337\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg.jpg 838w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg-300x74.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg-768x189.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg-400x98.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/04\/html_uploads-jpg-800x197.jpg 800w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><figcaption class=\"wp-element-caption\">\ud30c\uc77c \uc5c5\ub85c\ub4dc \uc0c1\ud0dc \ud655\uc778 \/ Check file upload status<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb httplib \uc6f9\uc11c\ubc84\uc5d0\uc11c multipart \ud30c\uc77c \uc5c5\ub85c\ub4dc\ub97c \uad6c\ud604\ud558\ub294 \uc124\uba85\uc785\ub2c8\ub2e4.This is a description of implementing multipart file uploads on an httplib web server. \ud83d\udc49\ud83c\udffb \ud504\ub85c\uc81d\ud2b8 \ub514\ub809\ud1a0\ub9ac\ub0b4\uc5d0 httplib.h\ud30c\uc77c\uc744 \ub2e4\uc6b4 \ubc1b\uc2b5\ub2c8\ub2e4.Download the httplib.h file into the project directory. \u2714\ufe0f httplib.h(0.42.0) \ud83d\udc49\ud83c\udffbHttp server basic (upload_server.cpp) \u2714\ufe0f \uc544\ub798\ub294 cpp \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uc11c \ub85c\uceec \uc11c\ubc84\ub85c \ud30c\uc77c\uc744 \uc5c5\ub85c\ub4dc \ud558\ub294 \uc124\uba85\uc785\ub2c8\ub2e4.The following is an explanation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5324","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5324"}],"version-history":[{"count":13,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5324\/revisions"}],"predecessor-version":[{"id":5377,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5324\/revisions\/5377"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}