-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ENG 09 1 File Handler
File parsing is extracting the file (or files) from a multipart-data POST request to an HttpFile
object through MultiPartParser
, here is some information about:
It is the object that you will use to extract and temporarily store the request files.
-
Receives the request object as a parameter, reads and identifies the files (if heard) and transfers it to the MultiPartParser variable.
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); }
-
Must be called after
parse()
, returns files of request in the format std::vector<HttpFile>.#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); // Check if have files if (fileParser.getFiles().empty()) { // No files found } size_t num_of_files = fileParser.getFiles().size(); }
-
Must be called after
parse()
, returns the list of other parts from the MultiPartData form.std::unordered_map<std::basic_string<char>> (key, value)
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); if (!fileParser.getFiles().empty()) { for (const auto &header : fileParser.getParameters()){ header.first // Key form header.second // Value from key form } } }
-
Must be called after
parse()
, individual version of getParameters().The type of the expected object (will be converted automatically), the key value of the parameter.
The content of the parameter corresponding to the key in the informed format, if it does not exist, will return the default value of the T Object.
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); std::string email = fileParser.getParameter<std::string>("email_form"); // Default type of string is "" if (email.empty()) { // email_form not found } }
It is the object that represents a file in memory, used by MultiPartParser
.
-
Self-explanatory name, gets the original name of the file that was received.
std::string.
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); std::string filename = fileParser.getFiles()[0].getFileName(); }
-
Gets the file size.
size_t
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); size_t filesize = fileParser.getFiles()[0].fileLength(); }
-
Gets the file extension.
std::string
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); std::string file_extension = fileParser.getFiles()[0].getFileExtension(); }
-
Get MD5 hash of file to check integrity.
std::string
-
Save the file to the file system. The folder saving the file is
UploadPath
configured in config.json (or equivalent). The full path isdrogon::app().getUploadPath()+"/"+this->getFileName()
Or to simplify, it is saved as: UploadPath/filename
-
Version if parameter is not omitted, uses the &path parameter instead of UploadPath.
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); // Relative path fileParser.getFiles()[0].save("./"); // Writes the file to the same directory on the server, with the original name // Absolute path fileParser.getFiles()[0].save("/home/user/downloads/"); // Writes the file in the indicated directory, with the original name }
-
Writes the file to the path parameter with a new name (ignores the original name).
#include "mycontroller.h" using namespace drogon; void mycontroller::postfile(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback) { // Only Post Requests (File Form) MultiPartParser fileParser; fileParser.parse(req); // Relative path fileParser.getFiles()[0].saveAs("./image.png"); // Same path of server /* Just example, Don't do this, you would overwrite the file format without checking if it really is png */ // Absolute path fileParser.getFiles()[0].save("/home/user/downloads/anyname." + fileParser.getFiles()[0].getFileExtension()); }
Next: Plugins
- Overview
- Install drogon
- Quick Start
- Controller
- Middleware and Filter
- View
- Session
- Database
- References
- Plugins
- Configuration File
- drogon_ctl Command
- AOP
- Benchmarks
- Coz profiling
- Brotli info
- Coroutines
- Redis
- Testing Framework
- FAQ