我想从我的iPhone应用程序上传图片到服务器。我正在使用AFNetworking v 2.6.3 请求内容类型是“application / x-www-form-urlencoded”。 我做了以下事情。
这是我的代码
let image = imageArray[0]
let pngImage = UIImagePNGRepresentation(image)
let imgString = pngImage?.base64EncodedString()
let parameters : NSDictionary = ["img" : imgString ?? "",
"name" : "imgName.png"]
let serviceName = ".../UploadBusinessImage"
WebserviceHandler.sharedWebserviceHandler.sendPOSTRequestStream(serviceName, parameters: parameters, successClosure: { (response) in
self.logResponse(response)
}) { (error ) in
self.logResponse(error.localizedDescription as AnyObject)
}
webserviceHandler is AFHTTPRequestOperationManager and set as below
class WebserviceHandler {
var webserviceHandler : AFHTTPRequestOperationManager!
class var sharedWebserviceHandler: WebserviceHandler {
struct Static {
static let instance: WebserviceHandler = WebserviceHandler()
}
return Static.instance
}
init() {
let baseURL = URL(string:"example.com/")!
self.webserviceHandler = AFHTTPRequestOperationManager(baseURL:baseURL)
self.webserviceHandler.requestSerializer = AFJSONRequestSerializer()
self.webserviceHandler.responseSerializer = AFJSONResponseSerializer()
}
func sendPOSTRequestStream(_ serviceName:String, parameters:NSDictionary, successClosure:@escaping WebserviceResultClosure, errorClosure:@escaping WebserviceErrorClosure){
self.webserviceHandler.post(serviceName, parameters: parameters, success: { (operation, responseObj) in
successClosure(responseObj as AnyObject)
}) { (operation, error) in
errorClosure(error as NSError)
}
}
但它不起作用。我该如何处理这样的请求?
除图像上传服务外,所有其他服务均为“application / json”