How to upload image with parameters using Alamofire in Swift

33,458

Solution 1

you can use Alamofire 3.0+ below code

func uploadImageAndData(){
    //parameters
    let gender    = "M"
    let firstName = "firstName"
    let lastName  = "lastName"
    let dob       = "11-Jan-2000"
    let aboutme   = "aboutme"
    let token     = "token"

    var parameters = [String:AnyObject]()
    parameters = ["gender":gender,
                  "firstName":firstName,
                  "dob":dob,
                  "aboutme":about,
                  "token":token,
                  "lastName":lastName]

    let URL = "http://yourserviceurl/"
    let image = UIImage(named: "image.png")

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in

        if let imageData = UIImageJPEGRepresentation(image, 0.6) {
            multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png")
        }

        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
    }, encodingCompletion: {
        encodingResult in

        switch encodingResult {
        case .Success(let upload, _, _):
            print("s")
            upload.responseJSON { 
                response in
                print(response.request)  // original URL request
                print(response.response) // URL response
                print(response.data)     // server data
                print(response.result)   // result of response serialization

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                }
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })
}

Solution 2

SWIFT 2 AlamoFire Simple Image Upload (REST API)

@amit gupta It seems answer contains big overhead. AlamoFire contain load of simplified solution. Alamofire.request method contains several simplified overload which can use to upload in simple manner. By using Alamofire.request( method developer can get rid of encoding overhead.

HTTP Status 415 gives because of not specifying the correct media type.

Please check my solution below.

import UIKit
import Alamofire

class ViewController: UIViewController {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var btnUpload: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func successDataHandler(responseData:String){

        print ("IMAGE UPLOAD SUCCESSFUL    !!!")

    }

    func failureDataHandler(errorData:String){

        print ("  !!!   IMAGE UPLOAD FAILURE   !!! ")

    }

    @IBAction func actionUpload(sender: AnyObject) {

        let URL = "http://m8coreapibeta.azurewebsites.net/api/cards/SaveImages"

        let postDataProlife:[String:AnyObject] = ["CardId":(dataCardDetail?.userId)!,"ImageType":1,"ImageData":imageView.image!]

        uplaodImageData(URL, postData: postDataProlife, successHandler: successDataHandler, failureHandler: failureDataHandler)
    }

    func uplaodImageData(RequestURL: String,postData:[String:AnyObject]?,successHandler: (String) -> (),failureHandler: (String) -> ()) -> () {

        let headerData:[String : String] = ["Content-Type":"application/json"]

        Alamofire.request(.POST,RequestURL, parameters: postData, encoding: .URLEncodedInURL, headers: headerData).responseString{ response in
            switch response.result {
            case .Success:
                print(response.response?.statusCode)
                successHandler(response.result.value!)
            case .Failure(let error):
                failureHandler("\(error)")
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Solution 3

Swift 4 with Alamofire 4

let isConnected = connectivity.isConnectedToInternet()

  func updateProfile(firstName:String,lastName:String ,imageData:Data?,completion: @escaping (isValidUser)->()) {


    if self.isConnected {

        var parameters : [String:String] = [:]
        parameters["auth_key"] = loginUser?.authKey!
        parameters["first_name"] = firstName
        parameters["last_name"] = lastName

        let url = "\(baseUrl)\(basicAuthenticationUrl.updateProfile)"
        print(url)


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            for (key, value) in parameters {
                multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
            }

            if let data = imageData {
                multipartFormData.append(data, withName: "image_url", fileName: "image.png", mimeType: "image/png")
            }

        }, usingThreshold: UInt64.init(), to: url, method: .post) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print("Succesfully uploaded  = \(response)")
                    if let err = response.error{

                        print(err)
                        return
                    }

                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }
    }

}

Solution 4

Almaofire with swift 3.0

Alamofire.upload(multipartFormData: { multipartFormData in
    var index = 1
    for image in imageArray {
        let imageData: Data = (UIImageJPEGRepresentation(image, 1.0) as Data?)!

        multipartFormData.append(imageData, withName: "home-\(index)", fileName: "home-\(index)", mimeType: "image/jpeg")

        index += 1
    }
    }, with: requestName, encodingCompletion: { result in
        switch result {
        case .success(let upload, _, _):

            upload.responseJSON { response in
                print("Image(s) Uploaded successfully:\(response)")
            }
        case .failure(let encodingError):
            print("encodingError:\(encodingError)")
        }
})
Share:
33,458

Related videos on Youtube

Reza_Rg
Author by

Reza_Rg

Mobile App(Android/iOS) developer.

Updated on July 09, 2022

Comments

  • Reza_Rg
    Reza_Rg almost 2 years

    I am developing an iPhone application with swift. and I'am using Alamofire framework for handling http requests. I use Alamofire.request for POST , GET and etc like this:

    Alamofire.request(.POST, myURL , parameters: ["a": "1", "b" : "2" ])
            .response { (request, response, data, error) in
    }  
    

    And I use Alamofire.upload to upload image to server this :

    Alamofire.upload(.POST, uploadURL , fileURL)
    

    And both works perfectly, but now I want to upload an image and also send some parameters with, and my content type should be multipart/form-data and Alamofire.upload does not accept parameters.

    There are two more question on SO about this issue with swift, which first one is not using Alamofire (and really, why not?) and in second one, mattt (Alamofire Developer) cited to use encoding parameters.

    I checked his example, but still couldn't figure out how to do that.

    Can any one please help me solve this problem?

    Thank you! :)

    • antiblank
      antiblank over 9 years
      Here's the solution I found, posted to another question: stackoverflow.com/questions/26121827/…
    • gutenmorgenuhu
      gutenmorgenuhu almost 9 years
      Reza_Rg Can you please help me? I also use 'Alamofire.upload(.POST, uploadURL , fileURL)', but how do I have to structure the php file to receive the file? Where is the file sent via .POST available in my php file?
    • user2363025
      user2363025 almost 9 years
      @Reza_Rg did you solve your problem, if so can you share an answer?
    • Reza_Rg
      Reza_Rg almost 9 years
      @user2363025 Yes, but I ended up changing some codes on Alamofire library, which I know is not the right thing to do.
  • amit gupta
    amit gupta about 8 years
    HTTP Error 415 Unsupported media type plz check image type @chamathjeevan
  • Deepak Thakur
    Deepak Thakur about 8 years
    If there is a key "image" in following parameter ["gender":gender,"firstName":firstName,"dob":dob,"aboutme":a‌​boutme,"token":token‌​,"lastName":lastName‌​, "image": imageData] then multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png") will append in parameters?
  • Deepak Thakur
    Deepak Thakur about 8 years
    @amitgupta I hope you got my question above
  • Shayan C
    Shayan C over 7 years
    Works great, one question If I take a picture from camera and perfrom UIImagePNGRepresentation function on UIImage, how do I preserve the orientation?
  • Harish Singh
    Harish Singh about 7 years
    It is not working for swift 3 even when changed the syntax a/c to swift 3
  • Saeed Rahmatolahi
    Saeed Rahmatolahi over 6 years
    my problem is images names and directories how to achieve images in the gallery directories and their names?
  • Harish Singh
    Harish Singh over 5 years
    Great@Dilip Tiwari