【发布时间】:2019-11-15 14:22:27
【问题描述】:
我正在尝试使用 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
}
}
}
【问题讨论】:
-
你能显示你想使用的代码吗?
-
添加了用于 alamofire 的代码