我正在尝试使用Alamofire上传图片,但它似乎无法上传。
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpg")
multipartFormData.append(operaID.data, withName: "id")
},
to: URL_CORDINATE)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print("Upload image response:", response.result.value)
}
case .failure(let encodingError):
print("Error while uploading image:", encodingError)
}
我期待看到图片上传,但身体似乎有问题(我应该将身体中的img和id作为参数传递!)
然而它确实在邮递员:https://i.stack.imgur.com/f0mUd.png
你可以试试这个:
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imgData!, withName: "img", fileName: "jpg", mimeType: "image/jpeg")
},
to: UPLOAD_IMAGE_URL,
encodingCompletion: { (encodingResult) -> Void in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
let result = response.data
do {
print("success")
}catch {
print("error")
}
}
case .failure(let encodingError):
print(encodingError)
}
})
首先看它缺少HTTP方法:.post
,mimeType应该是"image/jpeg"
,而不是"image/jpg"
。
看到这个:
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
multipartFormData.append(operaID.data, withName: "id")
},
to: YOUR_URL_HERE,
method: .post,
encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print("Upload image response:", response.result.value)
}
case .failure(let encodingError):
print("Error while uploading image:", encodingError)
}
})
是否有任何服务器日志指示错误?您是否可以在服务器代码中添加断点并在localhost上运行它来检查控制器中的内容?
如果服务器端点是http,请将其放在Info.plist
中:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Alamofire.upload(multipartFormData: { multipartFormData in
if let img = image, let imageData = img.jpegData(compressionQuality: 0.6) {
multipartFormData.append(imageData, withName: imageName, fileName: "temp.jpg", mimeType: "image/jpg")
}
for (key, value) in parameter ?? [:] {
if value is String || value is Int {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
}
}, to: url, headers: headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _ , _ ):
upload.responseJSON { response in
debugPrint(response)
if response.response?.statusCode == 200
{
if let jsonData = try? JSONSerialization.data(withJSONObject: response.result.value!, options: .prettyPrinted) {
print("Response: \n",String(data: jsonData, encoding: String.Encoding.utf8) ?? "nil")
}
success(response.result.value! as AnyObject)
}else
{
let res = response.result.value! as AnyObject
let msg = res["Message"] as? String
if msg != nil
{
failure(msg!)
}else
{
failure("")
}
}
}
case .failure(let encodingError):
print(encodingError)
failure(encodingError.localizedDescription)
}
})