Upload multiple file objects to API as 2d array of files
I am trying to build an API using django which support file uploads as an array of array. An example of what I am trying to achieve is,
[
{
"string": "Some string",
"files": [
"<uploaded file object 1>",
"<uploaded file object 2>"
]
},
{
"string": "Some string",
"files": [
"<uploaded file object 3>"
]
},
{
"string": "Some string",
"files": []
}
]
I dont want to use base64 encoded files for this since the files can be sometimes large, so I dont want to increase the overheads using base64.
**how can I achieve this API call in the most efficient way and what would be most appropriate structure to call this API and help with some javascript frontend code do it. **
I tried to move this to a FormData where I can acheive the a above upload like below but I am not able to call the API from the frontend, it doesnt work probably because I am writing wrong code.
strings: ["Some string", "Some string", "Some string"]
files: [["<uploaded file object 1>", "<uploaded file object 2>"], ["<uploaded file object 3>"]]
For my backend I am using django(rest framework), here is the relevant part of the serializer if required
class StringSerializer(serializers.Serializer):
serializers = serializers.ListField(child=serializers.CharField())
files = serializers.ListField(
child=serializers.ListField(child=serializers.FileField()),
write_only=True,
required=False,
allow_empty=True,
default=[],
)