alamofire multipartformdata use urlrequest

2019-02-24 22:55发布

I want to use Alamo fire multipart form data use request, for example, I use upload API

let profile = self.photoView.imageView?.image

    let parameters : [String:String] = [
        "homePageUrl": webURLField.text!,
        "nickName": nickNameField.text!,
        "selfIntro": introField.text!,
        ]

    let uri = Constants.APIURL.changeProfile
    let fileName = "\(nickNameField.text!).jpg"

    Alamofire.upload(multipartFormData: { (multipartFormData) in
         if let imageData = UIImageJPEGRepresentation(profile!, 1.0) {
            multipartFormData.append(imageData, withName: "profile", fileName: fileName, mimeType: "image/jpg")
        }

        for ( key, value ) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
        }

    }, usingThreshold: UInt64.init(), to: uri, method: .patch, headers: Constants.VyrlAPIConstants.getHeader(), encodingCompletion:
        {
            encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                 })

                upload.responseString { response in
                    if ((response.response?.statusCode)! == 200){
                        self.navigationController?.popViewController(animated: true)                            
                    }
                }
            case .failure(let encodingError):
                print(encodingError.localizedDescription)
            }
    })

this code sends 400 response from server log is.

PATCH 'http://blablabla.com:8080/users/profile':

Content-Type: multipart/form-data; boundary=alamofire.boundary.53a2e440bad1fabd X-Device: ios X-APP-Version: 1.0.0 Accept-Language: ko-kr 400 'http://blablabla.com:8080/users/profile' [0.0976 s]: X-Application-Context: application:dev Connection: close Transfer-Encoding: Identity Date: Thu, 13 Jul 2017 01:57:41 GMT

server want to receive http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is.. server log is.. (success 200 code)

curl -X PATCH --header 'Content-Type: multipart/form-data' --header 'Accept: /' --header 'X-APP-Version: 1.0.0' --header 'X-Device: ios' --header 'Accept-Language: ko-KR' {"type":"formData"} 'http://blablabla.com:8080/users/profile?nickName=abcd&selfIntro=hi%20my%20name%20is..'

definitely, i want to attach ?nicName=abcd use multiformpart data

How to use alamofire code?

3条回答
贼婆χ
2楼-- · 2019-02-24 23:38

In Swift 3 Alamofire 4

This is a demo Code for the Reference

Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.Imgprofile.image!, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
    }, to:" Your URL here ")
    { (result) in
        switch result {
        case .success(let upload, _, _):
            print(result)

            upload.uploadProgress(closure: { (progress) in
                print(progress)
            })

            upload.responseJSON { response in
                //print response.result
                print(response);
            }

        case .failure(let encodingError):
            print(encodingError);
        }
    }
}

Hope This should Work

Thanks

查看更多
三岁会撩人
3楼-- · 2019-02-24 23:59

if not work then make url with parameter like this.

 Alamofire.upload(multipartFormData:{ multipartFormData in
        multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")},
                     usingThreshold:UInt64.init(),
                     to:uri + "?" +nickNameField.text! +"?"+ webURLField.text!+"?"+introField.text!,
                     method:.post,
                     headers:Constants.VyrlAPIConstants.getHeader(),
                     encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload.responseJSON { response in
                                 print(response.result.value)
                            }
                        case .failure(let encodingError):
                            print(encodingError)
                        }
    })
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-24 23:59

Please Try This it's working for me for image upload

 var parameters = [String:AnyObject]()


    let profile = self.photoView.imageView?.image

    parameters = ["homePageUrl": webURLField.text as AnyObject,
                  "nickName": nickNameField.text as AnyObject,
                  "selfIntro": introField.text as AnyObject]


    let imgData = UIImageJPEGRepresentation(profile!, 0.2)!

    let uri = Constants.APIURL.changeProfile
    let fileName = "\(nickNameField.text!).jpg"


    Alamofire.upload(multipartFormData:{ multipartFormData in
        multipartFormData.append(imgData, withName: "profile",fileName: fileName, mimeType: "image/jpg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    },
                     usingThreshold:UInt64.init(),
                     to:uri,
                     method:.post,
                     headers:Constants.VyrlAPIConstants.getHeader(),
                     encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .success(let upload, _, _):
                            upload.responseJSON { response in
                                 print(response.result.value)
                            }
                        case .failure(let encodingError):
                            print(encodingError)
                        }
    })
查看更多
登录 后发表回答