我正在尝试使用swift将图像上传到服务器。我用NSMutableURLRequest
尝试了URLSession
。我收到网络连接丢失。我认为简单地使用Alamofire
是一种更好的方法,但是由于xcode
找不到函数update
而陷入了一个问题。
有人知道如何使用Alamofire上传图片吗?或找到更新功能?
alamofire的代码:
func uploadImageWithAlmofire(url: String) {
let params: Parameters = ["name": "abcd","gender": "Male"]
Alamofire.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(uiimagejpegrepresentation(self.yourimageView.image!,0.1)!,withName: "file",fileName: "file.jpeg",mimeType: "image/jpeg")
for (key,value) in params
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!,withName: key)
}
},to:url,headers:nil)
{ (result) in
switch result {
case .success(let upload,_,_ ):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON
{ response in
//print response.result
if response.result.value != nil
{
let dict :NSDictionary = response.result.value! as! NSDictionary
let status = dict.value(forKey: "status")as! String
if status=="1"
{
print("DATA UPLOAD SUCCESSFULLY")
}
}
}
case .failure(let encodingError):
break
}
}
}