Alamofire 4 上传参数 [英] Alamofire 4 upload with parameters
本文介绍了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)")
}
}
)
问题是在我的服务器上我没有看到 email
和 type
表单字段.为此,我遵循了在线发布的示例.我应该为此做些什么不同的事情吗?
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屋!
查看全文