我迁移到swift 3,我必须将Alamofire升级到4, 使用mulitpart请求上传图像在swift 2中工作正常,同样的API使用相同的参数获取内部服务器错误500成功 这里是swift 2.3代码,工作正常
Alamofire.upload(
.POST,
AppConstants.kAPIBaseURL + AppConstants.KAPIWidgetUrl + AppConstants.KAPIUpdateUserPhoto,headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData!, name: "image[image]", fileName: "myImage.png", mimeType: "image/png")
multipartFormData.appendBodyPart(data: "\((UserManager._currentUser?.userID)!)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"id")
multipartFormData.appendBodyPart(data: "profile_picture".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"upload_to")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { (JSON) in
let abc = JSON.result.value as! NSDictionary
print(abc)
let arrayOfResult = abc["results"] as! NSDictionary
let responseMessage = abc["message"] as! String
let currentUser: MOUser?
currentUser = MOUser.init(object: arrayOfResult)
UserManager.setCurrentUser(currentUser)
dispatch_async(dispatch_get_main_queue(),{
//Show Alert in UI
print("image uploaded");
[self.showAlertViewWithTitle(AppConstants.kEmptyString, message: "\(responseMessage)", dismissCompletion: {
})]
})
}
case .Failure(let encodingError):
print(encodingError);
}
}
);
以下是带错误的Swift 3代码
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData!, withName: "image[image]", fileName: "myImage.png", mimeType: "image/png")
multipartFormData.append("\(UserManager._currentUser?.userID)".data(using: .utf8, allowLossyConversion: false)!, withName: "id")
multipartFormData.append("profile_picture".data(using: .utf8, allowLossyConversion: false)!, withName: "upload_to")
},
to: AppConstants.kAPIBaseURL + AppConstants.KAPIWidgetUrl + AppConstants.KAPIUpdateUserPhoto ,method: .post, headers: headers,
encodingCompletion: { encodingResult in
print(encodingResult)
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print("SUCCESS RESPONSE: \(response)")
SVProgressHUD.dismiss()
if let info = response as? Dictionary<String, AnyObject> {
if let links = info["links"] as! Dictionary<String, AnyObject>? {
if let imgLink = links["image_link"] as? String {
print("LINK: \(imgLink)")
}
}
}
}
upload.uploadProgress { progress in
print(progress.fractionCompleted)
}
upload.responseString(completionHandler: { (response) in
print(response)
print(response.data)
print(response.debugDescription)
print(response.description)
print(response.result.description)
print(response.response)
})
case .failure(let encodingError):
SVProgressHUD.dismiss()
print("ERROR RESPONSE: \(encodingError)")
}//switch
}
);
服务器错误是这个
SUCCESS: {"status":"500","error":"Internal Server Error"}
SUCCESS
Optional(<NSHTTPURLResponse: 0x60000123cda0> { URL: https://../api///imageupload } { status code: 500, headers {
Connection = "keep-alive";
"Content-Length" = 48;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 11 Jan 2017 13:45:59 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.0.0/2015-12-16)";
Via = "1.1 vegur";
"X-Rack-Cache" = "invalidate, pass";
"X-Request-Id" = "78cc7e5c-9bc1-4802-beac-15f8f7c4400a";
"X-Runtime" = "0.167883";
如果有人可以帮我这个。谢谢!
答案 0 :(得分:1)
这一行有所不同:
multipartFormData.append("\(UserManager._currentUser?.userID)".data(using: .utf8, allowLossyConversion: false)!, withName: "id")`.
您不会像在2.3版本中那样强行打开UserManager._currentUser?.userID
:
multipartFormData.appendBodyPart(data: "\((UserManager._currentUser?.userID)!)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"id")
所以你的字符串&#34; id&#34;字段可能类似于&#34;可选(&#34;用户ID&#34;)&#34;而不仅仅是您期望的用户ID。