====== Introduzione a Crow a "A Fast and Easy to use microframework for the web." ====== Autore: **//Fabio Di Matteo//** \\ Ultima revisione: **// 21/01/2024 - 22:03 //** // // Il seguente codice c++ imposta delle rotte che mostrano alcune caratteristiche del framework. #include "crow.h" #include "crow/middlewares/cookie_parser.h" //#include "crow_all.h" int main() { //crow::SimpleApp app; crow::App app; //define your endpoint at the root directory CROW_ROUTE(app, "/")([](){ auto page = crow::mustache::load_text("index.html"); return page; }); CROW_ROUTE(app, "/ciao")([](){ auto t = crow::mustache::load_text("ciao.html"); const char* myname="Fabio Di Matteo"; auto page = crow::mustache::compile(t); crow::mustache::context ctx; ctx["name"] = myname; return page.render(ctx); }); // localhost:8080/req?p= CROW_ROUTE(app, "/req")([](const crow::request& req){ const char *page=req.url_params.get("p"); return page; }); CROW_ROUTE(app, "/cookies")([&app](const crow::request& req) { auto& ctx = app.get_context(req); std::string value = ctx.get_cookie("biscotto"); std::string page; if (value.empty()) { ctx.set_cookie("biscotto", "cioccolato"); page="Nessun cookie impostato."; }else{ page="Biscotto al gusto di : "+value; } return page; }); // download file (esempio: /download?f=my file.png) CROW_ROUTE(app, "/download")([](const crow::request& req,crow::response& res){ const char *f=req.url_params.get("f"); res.set_static_file_info(f); //res.set_header("Content-Disposition","filename=\"miofile.txt\""); res.end(); }); //set the port, set the app to run on multiple threads, and run the app app.port(8080).multithreaded().run(); } **makefile** all: g++ main.cc -o server ===== Upload di un file ===== Salvataggio di un file inviato attraverso un form multipart. ... CROW_ROUTE(app, "/save").methods(crow::HTTPMethod::POST)([=](const crow::request& req ){ std::string root_folder="/home/fabio/progetto"; crow::multipart::message msg(req); crow::multipart::header myheader = msg.parts[0].get_header_object("Content-Disposition"); //std::string myheader_value=myheader.value; std::string outfile_name=myheader.params["filename"]; CROW_LOG_ERROR << "File Name ->"+outfile_name; std::ofstream out_file(root_folder+"/"+outfile_name); if (!out_file) { CROW_LOG_ERROR << " Write to file failed\n"; return "Write to file failed!"; } out_file << msg.parts[0].body; out_file.close(); return "ok"; }); \ Piso - Upload files\ \ \ \

Upload a file


\
\ \ \
\