How to upload image using multipart request with Moya Swift?

前端 未结 2 1005
暖寄归人
暖寄归人 2021-02-04 10:58

I\'m using Moya 10.0.1 and I\'m having a problem when I\'m trying to upload an image to the server.

I did follow the Multipart Upload example and this is my setup code:<

2条回答
  •  心在旅途
    2021-02-04 11:44

        var task: Task {
        switch self {
        case .updateProfilePic(let memberID, let image):
            let imageData = UIImageJPEGRepresentation(image, 1.0)
            let memberIdData = memberID.data(using: String.Encoding.utf8) ?? Data()
            var formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: "user_img", fileName: "user.jpeg", mimeType: "image/jpeg")]
            formData.append(Moya.MultipartFormData(provider: .data(memberIdData), name: "member_id"))
            return .uploadMultipart(formData)
        }
    

    In Moya multipart request we have to pass parameter as multipartdata form along with it's key name.

提交回复
热议问题