I am using AFNetworking2
to send parameters and image to server.
manager.POST(urlPath, parameters: parameters, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
formData.appendPartWithFileData(imageData!, name: "image", fileName: "dummy.jpg", mimeType: "image/jpeg")
}, success: { (dataTask: NSURLSessionDataTask!, responseObject: AnyObject!) -> Void in
println("success: \(responseObject.description)")
}, failure: { (dataTask: NSURLSessionDataTask!, error: NSError!) -> Void in
println("failure: \(error)")
})
On server side, the data will be a dictionary
merged by parameters(QueryDict)
and the image data(MultiValueDict)
:
data=MergeDict(<QueryDict: {u'owner': [u'6'], u'description': [u'this
is p1'], u'name': [u'p1']}>, <MultiValueDict: {u'image':
[<InMemoryUploadedFile: file.jpg (image/jpeg)>]}>)
I reckon the 'MultiValueDict' is from this part of code:
formData.appendPartWithFileData(imageData!, name: "image", fileName: "dummy.jpg", mimeType: "image/jpeg")
However, I wanted to have MultiValueDict
like this:
{u'groupImages': [{u'image': [<InMemoryUploadedFile: file.jpg (image/jpeg)>]}]}
The data format is a Dictionary with an array value, and the array has another Dictionary value.
So what can I do to make formData.appendPartWithFileData
become such above data format?
EDIT:
I have seen some posts similar to my question. For example this one: AFNetworking post image in nested json
I have tried to change my code like this:
formData.appendPartWithFileData(imageData!, name: "groupImages[0].image", fileName: "dummy.jpg", mimeType: "image/jpeg")
or
formData.appendPartWithFileData(imageData!, name: "groupImages[0][image]", fileName: "dummy.jpg", mimeType: "image/jpeg")
but none of them worked for me.
My server expects to receive a JSON like this:
{
"name": "p2",
"owner": 6,
"description": "this is p2",
"groupImages": [{
"image": <InMemoryUploadedFile: dummy.jpg (image/jpeg)>
}]
}
Any idea?