我正在做以下内容上传带参数的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)")
}
}
)
Run Code Online (Sandbox Code Playgroud)
问题是在我的服务器上我没有看到email
和type
表单字段.我按照在线发布的示例进行了操作.有什么我应该做的不同吗?
编辑
如果我删除我放置的部分:
multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")
Run Code Online (Sandbox Code Playgroud)
然后包括参数.否则,我认为这是Alamofire 4.0.1中的一个错误.
Ekt*_*iya 43
它在我身边工作得很好.
我正在使用以下代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21812 次 |
最近记录: |