Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example for uploading files #638

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ add_executable(example_session middlewares/example_session.cpp)
add_warnings_optimizations(example_session)
target_link_libraries(example_session PUBLIC Crow::Crow)

add_executable(example_file_upload example_file_upload.cpp)
add_warnings_optimizations(example_file_upload)
target_link_libraries(example_file_upload PUBLIC Crow::Crow)

if(MSVC)
add_executable(example_vs example_vs.cpp)
add_warnings_optimizations(example_vs)
Expand Down
72 changes: 72 additions & 0 deletions examples/example_file_upload.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "crow.h"

int main()
{
crow::SimpleApp app;

CROW_ROUTE(app, "/uploadfile")
.methods(crow::HTTPMethod::Post)([](const crow::request& req) {
crow::multipart::message file_message(req);
for (const auto& part : file_message.part_map)
{
const auto& part_name = part.first;
const auto& part_value = part.second;
CROW_LOG_DEBUG << "Part: " << part_name;
if ("InputFile" == part_name)
{
// Extract the file name
auto headers_it = part_value.headers.find("Content-Disposition");
if (headers_it == part_value.headers.end())
{
CROW_LOG_ERROR << "No Content-Disposition found";
return crow::response(400);
}
auto params_it = headers_it->second.params.find("filename");
if (params_it == headers_it->second.params.end())
{
CROW_LOG_ERROR << "Part with name \"InputFile\" should have a file";
return crow::response(400);
}
const std::string outfile_name = params_it->second;

for (const auto& part_header : part_value.headers)
{
const auto& part_header_name = part_header.first;
const auto& part_header_val = part_header.second;
CROW_LOG_DEBUG << "Header: " << part_header_name << '=' << part_header_val.value;
for (const auto& param : part_header_val.params)
{
const auto& param_key = param.first;
const auto& param_val = param.second;
CROW_LOG_DEBUG << " Param: " << param_key << ',' << param_val;
}
}

// Create a new file with the extracted file name and write file contents to it
std::ofstream out_file(outfile_name);
if (!out_file)
{
CROW_LOG_ERROR << " Write to file failed\n";
continue;
}
out_file << part_value.body;
out_file.close();
CROW_LOG_INFO << " Contents written to " << outfile_name << '\n';
}
else
{
CROW_LOG_DEBUG << " Value: " << part_value.body << '\n';
}
}
return crow::response(200);
});

// enables all log
app.loglevel(crow::LogLevel::Debug);

app.port(18080)
.multithreaded()
.run();

return 0;
}