我是IOS的新手,n要将带有参数的图像上传到服务器,但没有上传。我尝试了很多,同时推荐alamofire和base64,但没有任何工作对我有用。我是否想念任何东西,有什么帮助吗?
先谢谢了。请帮助我解决问题
下面是我的代码:
@IBAction func submitBtnClicked(_ sender: Any)
{
//parameters
var number: Int?
number = 47000482
var srNumber = String(format:"%d",number!)
var urlString: String = filePath!.absoluteString
var parameters = ["s_no":srNumber, "count":"1"]
let imgData = UIImageJPEGRepresentation(self.chosenImage!, 0.5)
print(parameters)
print(imgData)
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imgData!, withName:"filestream", fileName:(self.filePath?.lastPathComponent)!, mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName:key)
}
}, to:"https://********************")
{ (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
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
}
答案 0 :(得分:0)
FLOOR((RAND([5,10,15,20,25,30,35,40]))
答案 1 :(得分:0)
首先,您需要添加方法类型(即.post或.put类型以及url),然后才能发送图像数据,并增加会话 manager.session请求的超时间隔。 configuration.timeoutIntervalForRequest = 120 。如果您有任何疑问,请在同一时间回复我,以便我尝试解决您的问题。
class func requestForAPI(param:Dictionary<String, String>?, Url:String,image:UIImage,isShowLoader:Bool, headers:[String:String]?, completion: @escaping (_ result: AnyObject?, _ error: Error?) -> Void) {
let reach:Reachability = Reachability.forInternetConnection()
let netStatus:NetworkStatus = reach.currentReachabilityStatus()
if (netStatus == NotReachable)
{
NSUtility.shared().showMessage(title: "Alert!", body: "Please connect to the internet to continue", themetype: .error)
return
}
if isShowLoader == true{
APPDELEGATE?.showLoaderView()
}
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 120
let imageData = image.jpegData(compressionQuality: 0.8)
manager.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(imageData, withName: "file", fileName: "file.jpeg", mimeType: "image/jpeg") for (key, value) in param {
multipartFormData.append(value.data(using:String.Encoding.utf8.rawValue)!, withName: key)
}, to:URL(string: Url)!,method:.put, headers:headers)
{ (result) in
switch result {
case .success(let upload,_,_ ):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON
{ response in
APPDELEGATE?.dismissLoader()
//print response.result
if response.result.value != nil
{
print (response.result.value as Any)
completion(response.result.value as AnyObject? , nil)
}
}
case .failure(let encodingError):
print(encodingError)
completion(nil, encodingError)
break
}
}}}