我正在尝试使用POST请求上传多个图像,方法如下:
file[] : photo_1.jpg
file[] : photo_2.jpg
.
.
file[] : photo_n.jpg
文件上传采用数组形式。这就是我想知道的原因
代码:
func submitPhoto(onCompletion: () -> (), onError: ((NSError) -> ())? = nil) {
guard let endPoint = Data.sharedInstance.submitPhotoEndpoint
else { print("Empty endpoint"); return }
let user = Users()
let Auth_header = [
"Authorization" : user.token,
]
let image = UIImage(named: "myImage")!
let parameters = [
"submission_id": "27",
"comment": "This is a comment"
]
// Begin upload
Alamofire.upload(.POST, endPoint, headers: Auth_header, multipartFormData: { multipartFormData in
// import image to request
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "http://public/uploads/1462026701_ir6.jpg", mimeType: "image/jpg")
}
// import parameters
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
}, // you can customise Threshold if you wish. This is the alamofire's default value
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
})
}