我正在做以下内容上传带参数的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中的一个错误.
我已完成图像上传,并使用以下方法成功上传到服务器上.
现在我想上传图像及其进度,以便有人告诉我该怎么做?我在各处找到但没有得到正确的解决方案.
图像上传代码没有它的进展:
@IBAction func uploadClick(_ sender: AnyObject) {
// define parameters
let parameters = [
"file_name": "swift_file.jpeg"
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, 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://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// …Run Code Online (Sandbox Code Playgroud)