I am trying to upload images together with other post parameters using Alamofire.
Looking at the doc I see:
Alamofire.upload(
.POST,
"https://httpbin.org/post",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
But how can I upload an array of images like:
var arrayOfImages: [UIImage] = [some images...]
together with other post data:
let parameters: [String: AnyObject] = [
"id": id,
"title": title,
"desc": description,
"images[]": arrayOfImages
]
func uploadProfileGIF(imageArray: NSMutableArray){
var count = 0
let bucketname = DAO.getBucketNameForLoggedInUser()
let uploadUrl = "https://whatever.com/whatever"
for i in imageArray{
var objectname = "picture" + String(count)
let image = i;
//Turn image into data
let imageData: NSData = UIImagePNGRepresentation(image as! UIImage)!
let params = ["objectname" : objectname, "bucketname" : bucketname!, "isGifImgae" : "True", "content_type" : "image/jpeg"]
let manager = AFHTTPSessionManager()
manager.POST(uploadUrl, parameters: params, constructingBodyWithBlock: { (AFMultipartFormData) in
AFMultipartFormData.appendPartWithFileData(imageData, name: "file", fileName: "image", mimeType: "image/jpeg")
}, progress: nil, success: { (s:NSURLSessionDataTask, response) in
print(response)
}) { (s:NSURLSessionDataTask?, e:NSError?) in
print(e)
}
count+=1
}
}
This is the way I did it. Hope this helps. Its AlamoFire, and it is a POST request, just a different syntax I guess. Just a for loop that makes an API call for every image in your array.
EDIT:
So instead of using a for loop and calling the API five separate times, simply change the name of the AFMultipartofrmdata. Pass in 5 files then in your API when you access the files, just access them by name. For example, the way that my API grabs the file, which is in Python, is like so
data = self.request.get('file')
Now data is my image, and I can upload it as such. Simply do this five different times in the same API call.
AFMultipartFormData.appendPartWithFileData(imageData1, name: "file1", fileName: "image", mimeType: "image/jpeg")
AFMultipartFormData.appendPartWithFileData(imageData2, name: "file2", fileName: "image", mimeType: "image/jpeg")
AFMultipartFormData.appendPartWithFileData(imageData3, name: "file3", fileName: "image", mimeType: "image/jpeg")
AFMultipartFormData.appendPartWithFileData(imageData4, name: "file4", fileName: "image", mimeType: "image/jpeg")
AFMultipartFormData.appendPartWithFileData(imageData5, name: "file5", fileName: "image", mimeType: "image/jpeg")
Then just assign the names and NSData inside of a for loop.
Then on the API side, something likes this;
image1 = self.request.get('file1')
image2 = self.request.get('file2')
image3 = self.request.get('file3')
image4 = self.request.get('file4')
image5 = self.request.get('file5')
Hope this helps! This what I changed my API call to instead of calling 5 different times for every user.