Подтвердить что ты не робот

Загрузка Alamofire 4 с параметрами

Я делаю ниже, чтобы загрузить PNG файл с параметрами:

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

            // Send parameters
            multipartFormData.append((UserDefaults.standard.value(forKey: Email) as! String).data(using: .utf8)!, withName: "email")
            multipartFormData.append("png".data(using: .utf8)!, withName: "type")

        },
        to: "user/picture",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
            case .failure(let encodingError):
                self.removeSpinnerFromView()
                print("ERROR RESPONSE: \(encodingError)")

            }
        }
    )

Проблема в том, что на моем сервере я не вижу поля формы email и type. Я следил за примерами, опубликованными в Интернете для этого. Есть ли что-то, что я должен сделать по-другому для этого?

ИЗМЕНИТЬ

Если я удалю часть, в которую я положил:

multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

THEN параметры включены. В противном случае, я думаю, что это ошибка в Alamofire 4.0.1.

4b9b3361

Ответ 1

Его работа отлично работает на моей стороне.

Я использую следующий код:

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

Ответ 2

Если ваше значение имеет тип Any, вы можете его изменить следующим образом:

for (key, value) in params {
    let paramsData:Data = NSKeyedArchiver.archivedData(withRootObject: value)
    formData.append(paramsData, withName: key)
}

Ответ 3

enter image description here

Модуль 'Alamofire' не имеет члена с именем 'upload'

Alamofire.upload(multipartFormData: { (multipartFormData) in
                for (key, value) in param {
                    multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as! String)
                }


                multipartFormData.append(self.get_imagedata, withName: "image_url", fileName: "image.png", mimeType: "image/png")


            }, usingThreshold: UInt64.init(), to: "\(constant.Post_rating)", method: .post) { (result) in
                switch result{
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print("Succesfully uploaded  = \(response)")
                        if let err = response.error{

                            print(err)
                            return
                        }

                    }
                case .failure(let error):
                    print("Error in upload: \(error.localizedDescription)")

                }
            }
            }
        }