X Tutup
/* This file is part of libhttpserver Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include class file_upload_resource : public httpserver::http_resource { public: std::shared_ptr render_GET(const httpserver::http_request&) { std::string get_response = "\n"; get_response += " \n"; get_response += "
\n"; get_response += "

Upload 1 (key is 'files', multiple files can be selected)


\n"; get_response += " \n"; get_response += "

\n"; get_response += "

Upload 2 (key is 'files2', multiple files can be selected)


\n"; get_response += "

\n"; get_response += " \n"; get_response += "
\n"; get_response += " \n"; get_response += "\n"; return std::shared_ptr(new httpserver::string_response(get_response, 200, "text/html")); } std::shared_ptr render_POST(const httpserver::http_request& req) { std::string post_response = "\n"; post_response += "\n"; post_response += " \n"; post_response += "\n"; post_response += "\n"; post_response += " Uploaded files:\n"; post_response += "

\n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; post_response += " \n"; for (auto &file_key : req.get_files()) { for (auto &files : file_key.second) { post_response += " \n"; } } post_response += "
KeyUploaded filenameFile system pathFile sizeContent typeTransfer encoding
"; post_response += file_key.first; post_response += ""; post_response += files.first; post_response += ""; post_response += files.second.get_file_system_file_name(); post_response += ""; post_response += std::to_string(files.second.get_file_size()); post_response += ""; post_response += files.second.get_content_type(); post_response += ""; post_response += files.second.get_transfer_encoding(); post_response += "


\n"; post_response += " back\n"; post_response += "\n"; return std::shared_ptr(new httpserver::string_response(post_response, 201, "text/html")); } }; int main(int argc, char** argv) { // this example needs a directory as parameter if (2 != argc) { std::cout << "Usage: file_upload " << std::endl; std::cout << std::endl; std::cout << " file_upload: writeable directory where uploaded files will be stored" << std::endl; return -1; } std::cout << "CAUTION: this example will create files in the directory " << std::string(argv[1]) << std::endl; std::cout << "These files won't be deleted at termination" << std::endl; std::cout << "Please make sure, that the given directory exists and is writeable" << std::endl; httpserver::webserver ws = httpserver::create_webserver(8080) .no_put_processed_data_to_content() .file_upload_dir(std::string(argv[1])) .generate_random_filename_on_upload() .file_upload_target(httpserver::FILE_UPLOAD_DISK_ONLY); file_upload_resource fur; ws.register_resource("/", &fur); ws.start(true); return 0; }
X Tutup