Swift - HTTP load failed (error code: -1005 [4:-4]), while uploading image through alamofire.

烂漫一生 提交于 2019-12-02 01:09:56

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..

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)
                }
            }
        )
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!