Upload images Multipart With Moya Swift Fully example

6.3k views Asked by At

how to upload an image using multipart/form-data in Swift With Moya

my code

var multipartBody: [MultipartFormData]? {
    switch self {
    case .registerWith(let registrationObject ):
        if let profileImageData = registrationObject.profileImageData{
        return [MultipartFormData(provider: .data(profileImageData), name: "profile_picture", fileName: "\(registrationObject.phone).jpg",
            mimeType: "image/jpeg")]
        }
        return nil
    default:
        return nil
    }
}

var task: Task {
    switch self {
    case .registerWith:
        if let multipartBody = multipartBody {
            return .upload(.multipart(multipartBody))
        }else{
            return .request
        }
    default:
        return .request
    }
}

but I need to send the imageData in body With key "profileImage"

3

There are 3 answers

0
Magdy Zamel On BEST ANSWER

To upload an image in Multipart using Moya version 10.0.0 and above

1- Put files you want to upload in MultipartFormData Array

// put files in MultipartFormData Arry  
   var multipartBody :[Moya.MultipartFormData]?{
    switch self {
    case .registerWith(let imageData , let name ,let description  ):
        // any additional body data or body parms
        let nameDataProvider = MultipartFormData(provider: .data(name.data(using: .utf8)!), name: "name")
        let emailDataProvider = MultipartFormData(provider: .data(email.data(using: .utf8)!), name: "email")
        // image want to upload it or file as a MultipartFormData
        let imageDataProvider = Moya.MultipartFormData(provider: MultipartFormData.FormDataProvider.data(imageData), name: "avatar", fileName: "photo.jpg", mimeType: "image/jpeg")
            return [ imageDataProvider , descriptionDataProvider ]
    default:
        return []
    }
}

2- make task type of this method is uploadMultipart

var task: Task {
    switch self {
    case .registerWith:
        // for each method multi part use its multipartBody that declared it in step one needed
        return .uploadMultipart(multipartBody!)
    default:
        guard let parameters = self.parameters else {
            return .requestPlain
        }
        return .requestParameters(parameters: parameters.values, encoding: parameters.encoding)
    }
}

To upload an image in Multipart using Moya version 9.0.0 and below

1- Put files you want to upload in MultipartFormData Array

// put files in MultipartFormData Arry  
var multipartBody: [MultipartFormData]? {
     switch self {
          case .registerWith(let imageData , let name ,let description  ):
              return [MultipartFormData(provider: .data(imageData),
             name: "profile_picture", fileName: "fileName", mimeType: "image/jpeg")]
              default:
               return nil
       } 
}

2- Add a provider of multipartBody to request's body of the method you need it

var parameters: [String: Any]? {
     switch self {
          case .registerWith(let registrationObject ):
          var body = registrationObject.toJSON()!
          // add the file in body 
          body["profile_picture"] = multipartBody?[0].provider
          return body
     }
}

3- make task type of this method is upload

var task: Task {
       switch self {
           case .registerWith:
              //  multipartBody that in First step
             return .upload(.multipart(multipartBody))
        }
}
1
HoangTung On

you need to change "name: "profile_picture"" to "name: "profileImage"", try this.

0
Hardik Thakkar On

Hear is code to upload image using Moya in swift 4.2

1) In task part of moya change like below function

var task: Task {
        switch self {

        case .signup(let params):
            var formData = [MultipartFormData]()
            for (key, value) in params {
                if let imgData = value as? Data {
                    formData.append(MultipartFormData(provider: .data(imgData), name: key, fileName: "testImage.jpg", mimeType: "image/jpeg"))
                } else {
                    formData.append(MultipartFormData(provider: .data("\(value)".data(using: .utf8)!), name: key))
                }
            }
            return .uploadMultipart(formData)

        case .signIn(let params), .resetPassword(let params), .changePassword(let params), .socialLogin(let params):
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        }
    }

2) How to make parameter

        var params = [String:Any]()
        params["first_name"] = self.txtFirstName.text
        params["last_name"] = self.txtLastName.text
        params["email"] = self.txtEmailAddess.text
        params["password"] = self.txtPassword.text

        if self.imgProfilePic.image != nil {
            if let imgData:Data = self.imgProfilePic.image!.pngData() {
                params["picture"] = imgData
            }
        }
        else {
           params["picture"] = ""
        }

and further pass this parameter to API calling.