ALamofile Image Upload

var headers: HTTPHeaders = [:] headers["Authorization"] = MCShared.sharedInstance.token Alamofire.upload( multipartFormData: { multipartFormData in if let imageData = self.selectedImage.jpegData(compressionQuality: 0.8) { multipartFormData.append(imageData, withName: "picture",fileName : "attachment.jpeg" , mimeType : "image/jpeg") } multipartFormData.append((UserDefaults.standard.value(forKey: "username") as! String).data(using: .utf8)!, withName: "username" ) }, to: "\(MCShared.MainURL.URL)save_photo", headers: headers , encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in switch response.result { case .success(let value): print("responseObject: \(value)") self.progressHud.hide(animated: true) let data = JSON(value) if data["status"].boolValue == true { let banner = NotificationBanner(title: "Success", subtitle:data["message"].stringValue, style: .success) banner.show() }else{ let banner = NotificationBanner(title: "Error", subtitle:data["message"].stringValue, style: .danger) banner.show() } case .failure(let responseError): self.progressHud.hide(animated: true) let banner = NotificationBanner(title: responseError.localizedDescription, subtitle:"Something Went Wrong", style: .danger) banner.show() } }.uploadProgress { progress in // main queue by default print("Upload Progress: \(progress.fractionCompleted)") } return case .failure(let encodingError): self.progressHud.hide(animated: true) let banner = NotificationBanner(title: encodingError.localizedDescription, subtitle:"Something Went Wrong", style: .danger) banner.show() debugPrint(encodingError) } })

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.