我想使用alamofire将多个图像上传到服务器。我已经使用了belwo代码用于此目的。但问题是我不知道我如何传递文件名& file的参数名称。我不知道如何实现它。
func uploadMultiImage(_ method:Method,domainName: String, parameters:Parameters?, encoding:Encoding, headers: [String:String]?,imageData:Data)-> Void {
print("header is \(headers)")
Alamofire.upload(imageData, to: completeURL(domainName: domainName), method: method, headers: headers).uploadProgress(closure: {
progress in
self.progress(progress.fractionCompleted)
}
).responseJSON(completionHandler: {response in
print("response is \(response.description)")
print("response1 is \(response.debugDescription)")
print("response2 is \(response.result.description)")
})
}
请指导哪种方式最好?
答案 0 :(得分:1)
if let img = selectImageOutlet.image {
// IMAGE_SHAKE_BASE_URL = "https://post.imageshack.us/upload_api.php"
let imageShackUrl = NSURL(string: IMAGE_SHAKE_BASE_URL)!
//Convert to jpeg & compress by 80%(0.2)
let imgData = UIImageJPEGRepresentation(img, 0.2)!
let keyData = IMAGE_SHACK_API_KEY.dataUsingEncoding(NSUTF8StringEncoding)!
let keyJson = "json".dataUsingEncoding(NSUTF8StringEncoding)!
Alamofire.upload(.POST, imageShackUrl, multipartFormData: { MultipartFormData in
MultipartFormData.appendBodyPart(data: keyData, name: "key")
MultipartFormData.appendBodyPart(data: imgData, name: "fileupload", fileName: "image", mimeType: "image/jpg")
MultipartFormData.appendBodyPart(data: keyJson, name: "format")
},encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
if let info = response.result.value as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
print("LINK: \(imgLink)")
}
}
}
} case .Failure(let error):
print(error)
}
})}
http://blog.mingweitee.com/2016/04/09/swift-how-to-upload-images-using-alamofire/