Alamofire 4 upload with parameters

后端 未结 2 1680
忘了有多久
忘了有多久 2020-11-28 12:57

I\'m doing the below to upload a PNG file with parameters:

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormD         


        
相关标签:
2条回答
  • 2020-11-28 13:16

    If your value is of type Any, you can change it like this

    for (key, value) in params {
        let paramsData:Data = NSKeyedArchiver.archivedData(withRootObject: value)
        formData.append(paramsData, withName: key)
    }
    
    0 讨论(0)
  • 2020-11-28 13:33

    Its working fine on my side.

    I'm using following code:

    let parameters = [
                "file_name": "swift_file.jpeg"
            ]
    
    Alamofire.upload(multipartFormData: { (multipartFormData) in
                multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
                for (key, value) in parameters {
                    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
                }
                }, to:"http://sample.com/upload_img.php")
        { (result) in
            switch result {
            case .success(let upload, _, _):
    
                upload.uploadProgress(closure: { (progress) in
                    //Print progress
                })
    
                upload.responseJSON { response in
                    //print response.result
                }
    
            case .failure(let encodingError):
                   //print encodingError.description
            }
        }
    
    0 讨论(0)
提交回复
热议问题