How to Implement Complex Form Data?
Posted
by
SoulBeaver
on Programmers
See other posts from Programmers
or by SoulBeaver
Published on 2013-06-25T13:08:31Z
Indexed on
2013/06/25
16:28 UTC
Read the original article
Hit count: 346
I'm supposed to implement a relatively complex form that looks like follows, but has at least four more pages requiring the user to fill in all necessary information for the tracks:
This data will need to be sent to the server, which is implemented using Dropwizard. I'm looking for best practices on how to upload and send such a complex form with potentially dozens of songs to the server.
The simplest available solution I have seen is a simple multipart/form-data request with the following form schema (Source):
Client
<html>
<body>
<h1>File Upload with Jersey</h1>
<form action="rest/file/upload" method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" size="45" />
</p>
<input type="submit" value="Upload It" />
</form>
</body>
</html>
Server
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadTrack(final FormDataMultiPart multiPart) {
List<FormDataBodyPart> artists = multiPart.getFields("artist");
StringBuffer output = new StringBuffer();
for (FormDataBodyPart artist : artists)
output.append(artist.getValueAs(String.class));
List<FormDataBodyPart> tracks = multiPart.getFields("track");
for (FormDataBodyPart track : tracks)
writeToFile(track.getValueAs(InputStream.class), "Foo");
return Response.status(200).entity(output.toString()).build();
}
Then I have also read about file uploads via Ajax or Formdata (Mozilla HttpRequest) which allows for Posts in the formats application/x-www-form-urlencoded, multipart/form-data, or text/plain. I don't know which approach, if any, is best.
An ideal solution would be to utilize Jackson to convert a json string into my data objects, but I don't get the impression that this is possible with binary data.
© Programmers or respective owner