Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multipart form in Vertx

I have got a multipart form with some text fields and some upload files. I need to handle this multipart post request in vertx handler so that all the uploaded files (variable number) should be read in chunks (for memory effeciency purpose). The moment I read the chunks (in foreach loop), I want to stream that out directly to the file. For multipart with text fields, I want to simply store the values to my model object.

I am quite new to vertx and therefore looking for a code snippet to achieve this but couldnt find it anywhere on the vertx documentation.

like image 764
Obaid Maroof Avatar asked Sep 06 '25 03:09

Obaid Maroof


2 Answers

You should look at vertx-web. It contains exactly what you need:

router.route().handler(BodyHandler.create());
router.post("/some/path/uploads").handler(routingContext -> {
    MultiMap attributes = routingContext.request().formAttributes();
    // do something with the form data
    Set<FileUpload> uploads = routingContext.fileUploads();
    // Do something with uploads....
});

Hope this will help.

like image 176
cdelmas Avatar answered Sep 07 '25 21:09

cdelmas


You can process the text field and the files seperately. This could simplify your processing.

request.handler(buffer -> {
   // decode the text field here
   // ...
}).uploadHandler(upload -> {
   // read upload files in chunks here
   // ...
}).endHandler(v -> {
   // upload request end here
   // ...
});
like image 35
kissLife Avatar answered Sep 07 '25 23:09

kissLife