I was trying to upload multiple images in a single batch where I convert images into base64 strings, append them into a json and send. But when I append more than 3 or 4 images server gets null for json for some reason. I think it is because base64 strings are huge in length.
My requirement is like this, I want to upload multiple images which are under one upload id and each and every image should have its own tags.
My json was like this
{
"upload_id":[{
"tag1.tag2": "base64 string of image 1"
},
{
"tag3.tag4": "base64 string of image 2"
}]
}
and this is the code i used to make the json and upload
// Images are saved in the documents directory, image paths are saved in imgPathsArr array and tags are saved in tagsArr.
var uploadDict = [String: Any]()
var imgDictArr = [[String: Any]]()
for (indx,imgPath) in imgPathsArr.enumerated(){
do{
let imgData = try Data(contentsOf: URL(fileURLWithPath: imgPath))
let base64Str = imgData.base64EncodedString(options: .init(rawValue: 0))
let imageDict = [tagsArr[index]:base64Str]
imgDictArr.append(imageDict)
}catch let error{
print(error.localizedDescription)
}
}
uploadDict[nic!] = imgDictArr
Alamofire.request("url", method: .post, parameters: uploadDict, encoding: JSONEncoding.default, headers: nil).response { (response) in
//things after getting response
}
Any possible way to get my work done. Any help would be highly appreciated.