Alamofire 4上传参数

编程入门 行业动态 更新时间:2024-10-14 20:23:05
本文介绍了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:"sample/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上传参数

本文发布于:2023-11-26 02:21:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   上传   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!