Vertx3.0 File Upload with Metadata

1,146 views
Skip to first unread message

Agraj Mangal

unread,
Jul 8, 2015, 2:04:51 PM7/8/15
to ve...@googlegroups.com
Hi guys,

I'm trying to do a simple File Upload using Vertx 3.0. I've gone through the examples at [1] and [2] and essentially trying a combination of both, as the requirement is to upload the asset as well as send some metadata about the asset in the same request itself. That is, I would like to support the following HTML form

<form action="/form" ENCTYPE="multipart/form-data" method="POST" name="wibble">
    foo:<input type="text" name="foo"/><br>
    bar:<input type="text" name="bar"/><br>
    quux:<input type="text" name="quux"/><br>
    choose a file to upload:<input type="file" name="myfile"/><br>
    <input type="submit"/>
</form>

The complete code for my Verticle is at [3]
As you can see from [3] What I don't know is how to specify the request handlers in order to get both form attributes as well as the uploaded file in a single Request Handler.  

I also tried using Vertx-Web FileUpload by doing the following 

  router.post("/form").handler(BodyHandler.create().setMergeFormAttributes(true));
    router.post("/form")
      .handler(routingContext -> {

        Set<FileUpload> fileUploadSet = routingContext.fileUploads();
        Iterator<FileUpload> fileUploadIterator = fileUploadSet.iterator();
        while (fileUploadIterator.hasNext()){
          FileUpload fileUpload = fileUploadIterator.next();
          
          // How to get actual file from this fileupload ? 
        }
        
    });

But I dont know how to get the actual file from the FileUpload [4] object. I could get the filename, contentType but how to get the actual file that is being uploaded ?

I'm surely missing something really simple. Looking forward to advice :)

Regards,
Agraj

Tim Fox

unread,
Jul 8, 2015, 2:19:19 PM7/8/15
to ve...@googlegroups.com
On 08/07/15 19:04, Agraj Mangal wrote:
Hi guys,

I'm trying to do a simple File Upload using Vertx 3.0. I've gone through the examples at [1] and [2] and essentially trying a combination of both, as the requirement is to upload the asset as well as send some metadata about the asset in the same request itself. That is, I would like to support the following HTML form

<form action="/form" ENCTYPE="multipart/form-data" method="POST" name="wibble">
    foo:<input type="text" name="foo"/><br>
    bar:<input type="text" name="bar"/><br>
    quux:<input type="text" name="quux"/><br>
    choose a file to upload:<input type="file" name="myfile"/><br>
    <input type="submit"/>
</form>

The complete code for my Verticle is at [3]
As you can see from [3] What I don't know is how to specify the request handlers in order to get both form attributes as well as the uploaded file in a single Request Handler.  

I also tried using Vertx-Web FileUpload by doing the following 

  router.post("/form").handler(BodyHandler.create().setMergeFormAttributes(true));
    router.post("/form")
      .handler(routingContext -> {

        Set<FileUpload> fileUploadSet = routingContext.fileUploads();
        Iterator<FileUpload> fileUploadIterator = fileUploadSet.iterator();
        while (fileUploadIterator.hasNext()){
          FileUpload fileUpload = fileUploadIterator.next();
          
          // How to get actual file from this fileupload ? 
        }
        
    });

But I dont know how to get the actual file from the FileUpload [4] object. I could get the filename, contentType but how to get the actual file that is being uploaded ?

I'm surely missing something really simple. Looking forward to advice :)

Please take a look at the test suite for examples of this :)

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Agraj Mangal

unread,
Jul 9, 2015, 1:00:49 AM7/9/15
to ve...@googlegroups.com
Thanks Tim for pointing me in the right direction. For reference, the test case for FileUpload using Vertx-web could be found here - https://github.com/vert-x3/vertx-web/blob/master/src/test/java/io/vertx/ext/web/handler/BodyHandlerTest.java#L185
Reply all
Reply to author
Forward
0 new messages