You can encode multiple request parts in the POST
payload and process that with Spring Boot.
Let's say you need to pass in two things per file:
Blob
with the file contents
metadata
- an object that can hold whatever - title/comments - you name it.
FrontEnd
You can use anything to simulate FormData
params, this is in TypeScript:
let's say a document looks like this:
export interface NewDocument {
title: string,
comment: string,
author: string,
file: File
}
So the generating of the FormData might be like:
private getFormData(doc: NewDocument): FormData {
let formData: FormData = new FormData();
const json: Blob = new Blob([JSON.stringify(doc)], {type: 'application/json'});
formData.append('metadata', json); //adding the metadata
const blob: Blob = new Blob([doc.file[0]], {type: doc.file[0].type});
formData.append('file', blob); //adding the file contents
return formData;
}
You can then POST
the form data to a given endpoint.
BackEnd
You can specify the different RequestParts
you're expecting in the request
@PostMapping
public ResponseEntity<Void> uploadFile(@RequestPart("metadata") FileMetadata metadata,
@RequestPart("file") MultipartFile file) {
//process
}
FileMetadata
here is a custom Java POJO into which you can deserialize the JSON representation of the FrontEnd's NewDocument
You should be able to turn this into a multiples version.
@PostMapping
public ResponseEntity<Void> uploadFile(@RequestPart("metadata") FileMetadata[] metadatas,
@RequestPart("file") MultipartFile[] files) {
//process
}
The problem now is how to identify which metadata is for which file.
A simple way would be to have the filename encoded in the metadata object and use that for mapping.