阿拉莫菲尔斯威夫特3号+;PHP上传图像不工作
我试图在我的服务器上使用Alamofire和PHP上传一个图像,我在这里检查了所有不同的问题,但由于某种原因它无法工作。我已经用HTML测试了我的PHP文件,它成功地上传了一个图像,所以它的范围缩小到我的swift代码 斯威夫特:阿拉莫菲尔斯威夫特3号+;PHP上传图像不工作,php,swift,alamofire,Php,Swift,Alamofire,我试图在我的服务器上使用Alamofire和PHP上传一个图像,我在这里检查了所有不同的问题,但由于某种原因它无法工作。我已经用HTML测试了我的PHP文件,它成功地上传了一个图像,所以它的范围缩小到我的swift代码 斯威夫特: let URL = "http://example.com/post_pic.php" let imageData = UIImageJPEGRepresentation(imageView.image!, 1) Alamofire.upl
let URL = "http://example.com/post_pic.php"
let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData!, withName: "imageFile", mimeType: "image/jpeg")
}, to:URL)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
print("Failure:")
print(encodingError)
}
}
PHP:
我得到的答复是:
Optional(<NSHTTPURLResponse: 0x17022df20> { URL: http://example.com/post_pic.php } {
status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 25;
"Content-Type" = "application/json";
Date = "Wed, 25 Jan 2017 10:16:29 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Ubuntu)";
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.20";
} })
Optional(25 bytes)
SUCCESS
JSON: {
response = "file_error";
}
可选({URL:http://example.com/post_pic.php } {
状态代码:200,标题{
连接=“保持活动”;
“内容长度”=25;
“内容类型”=“应用程序/json”;
日期=“2017年1月25日星期三10:16:29 GMT”;
“保持活动”=“超时=5,最大=100”;
Server=“Apache/2.4.7(Ubuntu)”;
“X-Powered-By”=“PHP/5.5.9-1ubuntu4.20”;
} })
可选(25字节)
成功
JSON:{
response=“文件错误”;
}
我可以在我的应用程序中使用此代码
let URL = "http://example.com/post_pic.php"
let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
upload(multipartFormData: { multipartFormData in
multipartFormData.append(imageData!, withName: "imageFile", fileName: "file.jpg", mimeType: "image/jpeg")
},
to: URL, method: .post,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.validate()
upload.responseJSON { response in
if response.result.error != nil {
print("failure")
UIAlertView(title: "Fail", message: "Please retry again.", delegate: nil, cancelButtonTitle: "Ok").show()
} else {
print("success")
}
}
case .failure(let encodingError):
print(encodingError)
}
}
)
我可以在我的应用程序中使用此代码
let URL = "http://example.com/post_pic.php"
let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
upload(multipartFormData: { multipartFormData in
multipartFormData.append(imageData!, withName: "imageFile", fileName: "file.jpg", mimeType: "image/jpeg")
},
to: URL, method: .post,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.validate()
upload.responseJSON { response in
if response.result.error != nil {
print("failure")
UIAlertView(title: "Fail", message: "Please retry again.", delegate: nil, cancelButtonTitle: "Ok").show()
} else {
print("success")
}
}
case .failure(let encodingError):
print(encodingError)
}
}
)
multipartFormData.append(imageData!,名称:“imageFile”,文件名=xxxx.jpg,mimeType:“image/jpeg”)
its:multipartFormData.append(imageData!,名称:“imageFile”,文件名:“xxxx.jpg”,mimeType:“image/jpeg)您知道您的php代码不正确吗?$_FILES参数不正确,大多数变量未初始化?让我们来看看。multipartFormData.append(imageData!,名称为:“imageFile”,文件名为xxxx.jpg,mimeType:“image/jpeg”)
its:multipartFormData.append(imageData!,名称为:“imageFile”,文件名:“xxxx.jpg”,mimeType:“你知道你的php代码不正确吗?$\u FILES参数不正确,并且大多数变量未初始化?让我们来看看。