赞
踩
这里先给出oat++ 中获取用户上传文件的官方文档 官方网址
官方最简单的是以下这个
- #include "oatpp/core/data/stream/FileStream.hpp"
-
- ...
-
- ENDPOINT("POST", "/upload", upload, REQUEST(std::shared_ptr<IncomingRequest>, request)) {
- oatpp::data::stream::FileOutputStream fileOutputStream("/path/to/file");
- request->transferBodyToStream(&fileOutputStream); // transfer body chunk by chunk
- return createResponse(Status::CODE_200, "OK");
- }
用户只需要提供文件的位置即可。
如果想获取用户上传的文件名,可以用以下代码
- ENDPOINT("POST", "datasets/upload", upload, REQUEST(std::shared_ptr<IncomingRequest>, request)) {
- /* Prepare multipart container. */
- auto multipart = std::make_shared<multipart::PartList>(request->getHeaders());
- /* Create multipart reader. */
- multipart::Reader multipartReader(multipart.get());
-
- /* Configure to read part with name "part1" into memory */
-
- multipartReader.setPartReader("mimiz", multipart::createInMemoryPartReader(-1));
-
- /* Read multipart body */
- request->transferBody(&multipartReader);
- /* Print value of "part1" */
- auto part1 = multipart->getNamedPart("mimiz");
-
-
- /* Assert part is not null */
-
- OATPP_ASSERT_HTTP(part1, Status::CODE_400, "part1 is null");
-
- auto filename = part1->getFilename();
- OATPP_LOGD("test", "%s", filename.getValue("").c_str());
-
- oatpp::data::stream::FileOutputStream fileOutputStream(std::string("/root/pro_bz/data/" + filename.getValue("")).c_str());
- fileOutputStream.writeSimple(part1->getPayload()->getInMemoryData());
-
- return createResponse(Status::CODE_200, "OK");
- }
其中filename就是用户上传的文件名,当用户使用postman测试的时候一定要保证key为mimiz
成功!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。