alamofire multipartformdata use urlrequest

前端 未结 3 1135
北恋
北恋 2021-01-19 07:14

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 :          


        
相关标签:
3条回答
  • 2021-01-19 07:25

    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

    0 讨论(0)
  • 2021-01-19 07:29

    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)
                            }
        })
    
    0 讨论(0)
  • 2021-01-19 07:43

    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)
                            }
        })
    
    0 讨论(0)
提交回复
热议问题