Uploading File With Parameters Using Alamofire

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
case .failure(let error):

Related Topics

Leave a reply