how to upload file and pass data in parameter in Alamofire - swift 5
You should pass the image data in MultipartFormData
instead of passing with the other parameter in the parameter
dictionary.
Just change your code like below and check it is working or not.
//Remove profile pic param from parameters.
let parameters : Parameters = ["name" : name,"region":region,"activity_id":ActivityCode!,"organization_id":OrganizationCode!,"gender":gender,"job_id":JobCode!,"interest_id":IntrestedCode!,"organisation_name":organisation_name,"activity_name":activity_name,"bod":bod,"city":region,"email":email]
guard let url = URL(string: "\(UrlApi.url)\(UrlApi.crateUser)") else { return }
Alamofire.upload(multipartFormData: { (form: MultipartFormData) in
for (key, value) in parameters {
form.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = photo.jpegData(compressionQuality: 0.5) {
form.append(data, withName: "profile_pic", fileName: "photo.png", mimeType: "image/png")
}
}
Uploading multiple images(with parameters) using multipart form data - Alamofire 5
Try this method to upload multiple images
class func uploadImageWithURL(endPath : String, dictImage : [String:[UIImage]], parameter : [String : Any], header : HTTPHeaders? = nil, success : @escaping ( Any? )->Void, failure : @escaping (Error) -> Void){
let baseUrl = "your base URL"
let fullUrl = baseUrl + endPath
var headers = ["Content-type" : "multipart/form-data"]
if let header = header{
headers = header
}
let url = (try? URLRequest(url: fullUrl, method: .post, headers: headers))!
upload(multipartFormData: { (multipartData) in
for imagesData in dictImage {
for arrimage in imagesData.value{
multipartData.append(arrimage.jpegData(compressionQuality: 0.50)!, withName: imagesData.key, fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
}
}
for (key, value) in parameter {
multipartData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, with: url) { (resultData) in
switch resultData{
case .success(let upload, let streamingFromDisk, let fileURL):
print("File URL : \(String(describing: fileURL))")
print("Streaming From Disk : \(streamingFromDisk)")
upload.uploadProgress(closure: { (progress) in
print("Progress : \(progress.fractionCompleted)")
})
upload.responseJSON(queue: nil, options: .allowFragments, completionHandler: { (response) in
if let value = response.result.value
{
success(value)
}
})
case .failure(let error):
failure(error)
print(error)
}
}
}
Related Topics
How to Record Video of Front and Back Camera At a Time in Ios
How to Rotate a Uiimage 90 Degrees
What Is the Ibeacon Bluetooth Profile
How to Use Nsjsonserialization
How to Change the Name of an iOS App
Getting Current Device Language in Ios
Cfnetwork Sslhandshake Failed iOS 9
Converting String to Int With Swift
Undefined Symbols For Architecture Arm64
Ios 7 Status Bar Back to iOS 6 Default Style in Iphone App
Exc_Bad_Access Signal Received
Instantiate and Present a Viewcontroller in Swift
Fast and Lean Pdf Viewer For Iphone/Ipad/Ios - Tips and Hints
Applications Are Expected to Have a Root View Controller At the End of Application Launch
Status Bar and Navigation Bar Appear Over My View'S Bounds in iOS 7
How to Programmatically Get the MAC Address of an Iphone
Updating Configuration Profile Installed in iOS Device in Mdm