How to upload a large file with multipart using the TemporaryFile from 1.3.0

306 Views Asked by At

I'm trying to create an endpoint and corresponding swagger endpoint_info which uploads a file via multipart. I was hoping to use the TemporaryFile to write the parts then iterate the parts from getAllParts() to dump them to my final file. I can't seem to get the endpoint_info to create the right boundary in my request. I'm getting an exception when I try to create the PartList: Error. No 'boundary' value found in headers.

ENDPOINT_INFO(upload) {
        info->summary =
            "Upload"
        info->addResponse<Object<CommandResponseDto>>(Status::CODE_200,
                                                      "application/json");
        info->addConsumes<oatpp::String>("multipart/form-data");
    }

    ENDPOINT("POST", "upload",
             upload,
             REQUEST(std::shared_ptr<IncomingRequest>, request)) {
        namespace mp = oatpp::web::mime::multipart;
        try {
            mp::PartList multipart(request->getHeaders());
        } catch (const std::exception& e) {
            logger_->error("Error creating multipart object: {}", e.what());
            return create_error_response(e.what());
        }
        mp::PartList multipart(request->getHeaders());

        mp::Reader multipartReader(&multipart);

        multipartReader.setDefaultPartReader(
            mp::createTemporaryFilePartReader("/tmp" /* /tmp directory */));

        request->transferBody(&multipartReader);

        auto parts = multipart.getAllParts();
        for (auto& p : parts) {
            /* print part name and filename */
            logger_->error("Multipart", "Part name={}, filename={}",
                           p->getName()->c_str(), p->getFilename()->c_str());
            
            /* some append all files into one large file */
        }

        return createResponse("OK");
    }

I've tried searching around the docs and using both synchronous and asynchronous endpoints.

1

There are 1 best solutions below

0
Vy Vy On

I think maybe you have a mistake. When I create my request by Postman, using multipart to send files, it automatically calculated the boundary (I think this's related to the sizes of files when sending the request NOT the endpoint). It's in Content-Type header. Something like this:

Content-Type: "multipart/form-data; boundary=156466241443238208096850"

About the endpoint, I do the same as you. But I realize in my Windows, my code works with the path "tmp\\" instead of "/tmp".

Hope this helps. Sorry if my answer is so confusing. Because this's the first time I post.