Alamofire 4上传参数 [英] Alamofire 4 upload with parameters

查看:100
本文介绍了Alamofire 4上传参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下操作,以上传带有参数的PNG文件:

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

    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)")

            }
        }
    )

问题是在服务器上,我没有看到电子邮件类型表单字段。我遵循了在线发布的示例。我是否应该对此采取其他措施?

Problem is that on my server I don't see the email and type form fields. I followed examples posted online for this. Is there anything I should do differently for this?

编辑

如果我删除我放置的部分:

If I remove the part where I put:

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

当包含参数时。否则,我认为这是Alamofire 4.0.1中的错误。

THEN the parameters are included. Otherwise not, I think this is a bug in Alamofire 4.0.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
        }
    }

这篇关于Alamofire 4上传参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆