-1

I am trying to upload UIImage file to server by using .post and mulitpartFormData way. Anyone can look at below code and advise what is wrong? I have tried to succeed uploading image file for whole day today. But with my short experience and study of swift, even it was so difficult to research proper answer related to my situation.

PS. "UIImageView.image must be used from main thread only" message are shown continuously "guard let" point. Please also let me borrow your wisdom.

    Alamofire.upload(
            multipartFormData: { multipartform in
                guard let image = self.profileImage.image else { return }
                    let imageData = UIImageJPEGRepresentation(image, 0.1)
                    multipartform.append(imageData!, withName: "img_profile", mimeType: "image/jpg")

        },
            to: urlString,
            method: .post,
            encodingCompletion: { result in
                switch result {
                case .success(let request, _, _):
                    request.responseData(completionHandler: { (response) in
                        switch response.result {
                        case .success:
                            print(response)
                        case .failure(let error):
                            print(error)
                        }
                })
                case .failure(let error):
                    print(error)
                }
        })
picciano
  • 22,341
  • 9
  • 69
  • 82
  • what is the issue you are facing? – hardik parmar Apr 11 '18 at 16:29
  • I have tried to upload image file to server, but it has been failed so far. To be frankly, I have no idea what was wrong in my code. I have tested to upload a image to server by using Postman, it was working, so maybe the problem is in my coding I guess. – Yong Ho Kim Apr 11 '18 at 16:33
  • 1
    have you debugged the flow? using breakpoints? does any of the error or success gets printed? – hardik parmar Apr 11 '18 at 16:36
  • SUCCESS: { "img_profile" = ( "\Uc81c\Ucd9c\Ub41c \Ub370\Uc774\Ud130\Ub294 \Ud30c\Uc77c\Uc774 \Uc544\Ub2d9\Ub2c8\Ub2e4. \Uc81c\Ucd9c\Ub41c \Uc11c\Uc2dd\Uc758 \Uc778\Ucf54\Ub529 \Ud615\Uc2dd\Uc744 \Ud655\Uc778\Ud558\Uc138\Uc694." ); – Yong Ho Kim Apr 11 '18 at 16:43
  • Above is the output I could see. – Yong Ho Kim Apr 11 '18 at 16:44
  • this seems to be working. print 123 where you are printing the response in success block. if it gets printed there then it is working. try it and revert back – hardik parmar Apr 11 '18 at 16:46
  • Possible duplicate of [How to upload images to a server in iOS with Swift?](https://stackoverflow.com/questions/26335656/how-to-upload-images-to-a-server-in-ios-with-swift) – picciano Apr 11 '18 at 16:49
  • I do appreciate for your prompt reply and help. I solve the problem that I added file name as advised as below comment. – Yong Ho Kim Apr 12 '18 at 03:50

1 Answers1

0

Your problem is with the following line because you are not adding filename and filename will be the parameter name in which server expecting the image:

multipartform.append(imageData!, withName: "img_profile", mimeType: "image/jpg")

Try updating with this by adding file-name:

multipartFormData.append(imageData!, withName: "img_profile", fileName: "file-name", mimeType: "image/jpg")

Hope this will help.

Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36