我正在尝试上传base64string格式的图片。在这里,我正在使用alamofire和swift上传图像与一些参数。但是我的图片没有上传到服务器上,请检查我的代码,让我知道我哪里做错了。请检查我的代码,让我知道我哪里做错了。我的代码是
func postRegistrationPlayerProfile(urlExtension: String, callback: @escaping(Bool,String?) -> Void,parameters:[String:Any]) -> Void {
let fullURL = URL(string: urlExtension)!
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TimeInterval(Common.sharedInstance().REQUEST_TIME_OUT)
manager.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
}, usingThreshold: UInt64.init(), to: fullURL, method: .post) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { (response : DataResponse<Any>) in
let responseDataString = Common.sharedInstance().getJSONStringFromData(data: response.data! as NSData)
print(responseDataString)
if let result = response.result.value {
let dictionary = result as! NSDictionary
let status = dictionary["status"] as! String
let message = dictionary["message"] as? String
if status == "success"{
callback(true,message)
return
}
else{
callback(false,message)
return
}
}
else{
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
return
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
callback(false,Common.sharedInstance().FAILED_BANNERIMG_UPLOAD)
}
}
}
我的参数是:
paramters.updateValue(phone!, forKey: "phone")
paramters.updateValue(state!, forKey: "state")
paramters.updateValue(city!, forKey: "city")
paramters.updateValue(zip!, forKey: "postcode")
paramters.updateValue(travel, forKey: "travel_distance")
paramters.updateValue(base64ImageString!, forKey: "profile_image")
paramters.updateValue(country!, forKey: "country")
我正在将我的图片转换为base64字符串,如。
if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){
base64ImageString = data.base64EncodedString(options: .lineLength64Characters)
}
}
添加
url('data:image/jpeg;base64,)
到我的Base64转换后的字符串对我来说是有效的。
if let selectedImage = chooseProfileImage.image{
if let data = selectedImage.jpegData(compressionQuality: 1){
let imageString = selectedImage.jpegData(compressionQuality: 1.0)?.base64EncodedString() ?? ""
base64ImageString = "url('data:image/jpeg;base64,\(imageString)')"
profileData = data
}
}