3

Here the alamofire code i am using

params is a dictionary [String:Any]

Alamofire.upload(
    multipartFormData: { MultipartFormData in

        for (key, value) in params {
            if let image = value as? UIImage {
                if let imageData = UIImageJPEGRepresentation(image, 0.2) {
                    MultipartFormData.append(imageData, withName: "image", fileName: "file.jpg", mimeType: "image/jpg")
                }
            }else {
                MultipartFormData.append(String(describing: value).data(using: String.Encoding.utf8)!, withName: key)
            }
        }

}, to:url, method: .patch, headers: ["token": authToken,"Content-Type":"application/json"]) { (result) in

    switch result {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            guard response.result.isSuccess else {
                print(response.error?.localizedDescription ?? "Error while requesting")
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
            }
        }

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

I want to upload an image to the server, read some post for uploading via alamofire but none seems to be working for me? Please help me find the issue here.

Solutions given in the duplicate question arent working for me

Thank you

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • Possible duplicate of [upload image to server using Alamofire](https://stackoverflow.com/questions/40519829/upload-image-to-server-using-alamofire) – Hardik Thakkar Jun 15 '18 at 15:21
  • yea...tried but nothing works...do u think maybe server has the issue, cause i have tried everything that i hve encountered – Viraj Singh Jun 15 '18 at 15:44
  • what is this... method: .patch i didn't know much about this.. – Hardik Thakkar Jun 15 '18 at 17:28
  • check out this link i given details answer in this link with (method: .post) : https://stackoverflow.com/questions/40262201/how-to-upload-multiple-images-in-multipart-using-alamofire/43634314#43634314. – Hardik Thakkar Jun 15 '18 at 17:31

1 Answers1

0

Your content type is wrong. Use "Content-Type":"multipart/form-data"

miss Gbot
  • 363
  • 3
  • 9