问题
I am a newbie to iOS and using Alamofire to upload images. The code I have written is as follows:
let image = imageView.image
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let headers: HTTPHeaders = [
"x-access-token": "######",
"Accept": "application/json"
]
let parameters = ["profile_picture": "kinza"]
let url = try! URLRequest(url: URL(string:"######")!, method: .post, headers: headers)
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
with: url)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
When I run this code I get the following log:
2017-11-07 11:03:21.595826+0500 TestApiProject[2457:51089] [] nw_socket_get_input_frames recvmsg(fd 6, 4096 bytes): [54] Connection reset by peer
2017-11-07 11:03:21.596094+0500 TestApiProject[2457:51089] TIC Read Status [1:0x6040003612c0]: 1:54
2017-11-07 11:03:21.596495+0500 TestApiProject[2457:51089] [] nw_socket_output_finished shutdown(6, SHUT_WR): [57] Socket is not connected
2017-11-07 11:03:21.597203+0500 TestApiProject[2457:51089] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> HTTP load failed (error code: -1005 [4:-4])
2017-11-07 11:03:21.597681+0500 TestApiProject[2457:51091] Task <9A25E63E-EC42-419C-A0B7-02998177EDCA>.<1> finished with error - code: -1005
Upload Progress: 1.0
nil
It have searched but didn't find anything specific to 'error code -1005' How can i solve it? Did i miss anything?
I have certain questions regarding it:
- If http load failed then how did control go to success block of Alamofire request?
- The value of response.result.value is nil. Is something wrong on my end or on server side?
- Do I need to add some more attributes to my info.plist? This is how it is currently.
Any help would be much appreciated.
回答1:
Try This..
let image = imageView.image
let imgData = UIImageJPEGRepresentation(image, 0.2)!
let parameters = ["profile_picture": "kinza"]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "profile_picture",fileName: "kinza.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},to:"http://YOUR URL"){ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value!)
}
case .failure(let encodingError):
print(encodingError)
}
}
It's Works For Me..
回答2:
You can use this method as it worked for me:
fileprivate func p_uploadImage(_ image: UIImage) {
let parameters = ["channelName" : "Your text param"]
let fileData = UIImageJPEGRepresentation(image, 0.2)
let URL2 = try! URLRequest(url: "Your URL", method: .post, headers: ["Authorization" : authKey!])
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(fileData!, withName: "upfile", fileName: "upfile", mimeType: "text/plain")
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: URL2 , encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
print("s")
upload.responseJSON {
response in
if let JSON = response.result.value as? [String : Any]{
let messageString = JSON["message"] as? String
}else {
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
}
case .failure(let encodingError):
print(encodingError)
let alertError = UIAlertController(title: "Alert", message: "Image upload error", preferredStyle: UIAlertControllerStyle.alert)
alertError.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alertError, animated: true, completion: nil)
}
}
)
}
来源:https://stackoverflow.com/questions/47151184/swift-http-load-failed-error-code-1005-4-4-while-uploading-image-throu