upload image to server using Alamofire

别来无恙 提交于 2019-11-26 18:50:48
Ekta Padaliya

Try below code

 let image = UIImage.init(named: "myImage")
 let imgData = UIImageJPEGRepresentation(image!, 0.2)!

 let parameters = ["name": rname] //Optional for extra parameter

Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            } //Optional for extra parameters
    },
to:"mysite/upload.php")
{ (result) in
    switch result {
    case .success(let upload, _, _):

        upload.uploadProgress(closure: { (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
        })

        upload.responseJSON { response in
             print(response.result.value)  
        }

    case .failure(let encodingError):
        print(encodingError)  
    }
}
Chandan Taneja
let params: Parameters = ["name": "abcd" "gender": "Male"]
Alamofire.upload(multipartFormData:
    {
        (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.yourimageView.image!, 0.1)!, withName: "image", fileName: "file.jpeg", mimeType: "image/jpeg")
        for (key, value) in params
        {
            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
        }
}, to:yourUrl,headers:nil)
{ (result) in
    switch result {
    case .success(let upload,_,_ ):
        upload.uploadProgress(closure: { (progress) in
            //Print progress
        })
        upload.responseJSON
            { response in
                //print response.result
                if response.result.value != nil
                {
                    let dict :NSDictionary = response.result.value! as! NSDictionary
                    let status = dict.value(forKey: "status")as! String
                    if status=="1"
                    {  
                      print("DATA UPLOAD SUCCESSFULLY")
                    }
                }
        }                
    case .failure(let encodingError):
        break
    }   
}

Need to specify name, fileName, mimeType, these are important to many servers

func upload(image: UIImage, completion: (URL?) -> Void) {
    guard let data = UIImageJPEGRepresentation(image, 0.9) else {
      return
    }

    Alamofire.upload(multipartFormData: { (form) in
      form.append(data, withName: "file", fileName: "file.jpg", mimeType: "image/jpg")
    }, to: "https://yourawesomebackend.com", encodingCompletion: { result in
      switch result {
      case .success(let upload, _, _):
        upload.responseString { response in
          print(response.value)
        }
      case .failure(let encodingError):
        print(encodingError)
      }
    })
  }

Try below code For MultipleImage Upload. asked by @Saurabh.

However it is better to make upload 1 by 1 instead of uploading all at once.
because if it failed, it will only failed on 1 image. but upload all at once.if 1 file failed user need to restart the uploading process from the beginning.

nevertheless here is what you going have to do if you want to upload multiple data at once.

 let image1 = UIImage.init(named: "myImage1")
 let image2 = UIImage.init(named: "myImage2")
 let image3 = UIImage.init(named: "myImage3")
 let image4 = UIImage.init(named: "myImage4")
 let imgData1 = UIImageJPEGRepresentation(image!, 0.2)!
 let imgData2 = UIImageJPEGRepresentation(image!, 0.2)!
 let imgData3 = UIImageJPEGRepresentation(image!, 0.2)!
 let imgData4 = UIImageJPEGRepresentation(image!, 0.2)!


 let parameters = ["name": rname] //Optional for extra parameter

Alamofire.upload(multipartFormData: { multipartFormData in
        //loop this "multipartFormData" and make the key as array data
        multipartFormData.append(imgData1, withName: "fileset[0]",fileName: "file.jpg", mimeType: "image/jpg")
        multipartFormData.append(imgData2, withName: "fileset[1]",fileName: "file.jpg", mimeType: "image/jpg")
        multipartFormData.append(imgData3, withName: "fileset[2]",fileName: "file.jpg", mimeType: "image/jpg")
        multipartFormData.append(imgData4, withName: "fileset[3]",fileName: "file.jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            } //Optional for extra parameters
    },
to:"mysite/upload.php")
{ (result) in
    switch result {
    case .success(let upload, _, _):

        upload.uploadProgress(closure: { (progress) in
            print("Upload Progress: \(progress.fractionCompleted)")
        })

        upload.responseJSON { response in
             print(response.result.value)  
        }

    case .failure(let encodingError):
        print(encodingError)  
    }
}

Noted: multipartFormData are using append meaning it is an Array of request. you can loop and append more if needed.

Ok Bro I use this code with Swift 4 and Alamofire

import Foundation
import Alamofire

class UploadImageController: NSObject {

// MARK: - shared

static let shared  = UploadImageController()

// MARK: - init

let decoder = JSONDecoder()

// MARK: - uploadImageOnly

func uploadImageWith(endUrl: String, photo: UIImage?, parameters: [String : Any]?, headers: HTTPHeaders?, completion: @escaping (_ success: Bool, _ uploadImageResponse: UploadImageResponse?) -> Void ) {

    Alamofire.upload(multipartFormData: { (multipartFormData) in

        if let data = UIImageJPEGRepresentation(photo!, 0.5) {

            multipartFormData.append(data, withName: "invoice", fileName: "invoice.jpeg", mimeType: "invoice/jpeg")
        }

    }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: endUrl, method: .post, headers: headers) { (result) in

        switch result {

        case .failure(let error):
            print("UploadImageController.requestWith.Alamofire.usingThreshold:", error)
            completion(false, nil)

        case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })

            upload.responseJSON(completionHandler: { (response) in

                switch response.result {

                case .failure(let error):

                    print("UploadImageController.requestWith.Alamofire.upload.responseJSON:", error)

                    completion(false, nil)

                case .success( _):

                    print("UploadImageController.requestWith.Alamofire.upload.responseJSON Succes")
                    guard let data = response.data else { return }

                    do {

                        let uploadImageResponse = try self.decoder.decode(UploadImageResponse.self, from: data)

                        completion(true, uploadImageResponse)

                    } catch let jsonError {

                        print("Error serializing json.ProfileController.getProfile:", jsonError)
                        completion(false, nil)
                    }
                }
            })
        }
    }
}

// MARK: - uploadImageWithParameters

func uploadImageWithParametersAnd(endUrl: String, photo: UIImage?, parameters: [String : Any]?, headers: HTTPHeaders?, completion: @escaping (_ success: Bool, _ addInvoiceResponse: AddInvoiceResponse?) -> Void ) {

    Alamofire.upload(multipartFormData: { (multipartFormData) in

        if let data = UIImageJPEGRepresentation(photo!, 0.5) {

            multipartFormData.append(data, withName: "invoicePicture", fileName: "invoicePicture.jpeg", mimeType: "invoice/jpeg")
        }

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


    }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: endUrl, method: .post, headers: headers) { (result) in

        switch result {

        case .failure(let error):
            print("UploadImageController.requestWith.Alamofire.usingThreshold:", error)
            completion(false, nil)

        case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })

            upload.responseJSON(completionHandler: { (response) in

                switch response.result {

                case .failure(let error):

                    print("UploadImageController.requestWith.Alamofire.upload.responseJSON:", error)

                    completion(false, nil)

                case .success( _):

                    print("UploadImageController.requestWith.Alamofire.upload.responseJSON Succes")
                    guard let data = response.data else { return }

                    do {

                        let addInvoiceResponse = try self.decoder.decode(AddInvoiceResponse.self, from: data)

                        completion(true, addInvoiceResponse)

                    } catch let jsonError {

                        print("Error serializing json.ProfileController.getProfile:", jsonError)
                        completion(false, nil)
                    }
                }
            })
        }
    }
 }
}

For example this AddInvoiceResponse

import Foundation

struct AddInvoiceResponse: Decodable {
    let id, message: String?
}

and here UploadImageResponse

import Foundation

struct UploadImageResponse: Codable {
    let id, message: String?
}
    let url = BaseViewController.API_URL + "uploads"
    let image = info[UIImagePickerControllerEditedImage] as? UIImage
    let imgData = UIImageJPEGRepresentation(image!, 0.2)!

    let parameters = [
                            "user_id" : UserDefaults.standard.value(forKey: "userId")!
    ]

    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "uload_data",fileName: "file.jpg", mimeType: "image/jpg")
        for (key, value) in parameters {
            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
        } //Optional for extra parameters
    },
                     to:url)
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })

            upload.responseJSON { response in

                self.objHudHide()
                print(response.result.value)

                let jsonDict : NSDictionary = response.result.value as! NSDictionary

                print(jsonDict)
                if  jsonDict["status"] as! String == "Success"
                {


                    let detailDict : Dictionary = jsonDict["detail"] as! Dictionary<String,Any>

                    if let getTotalPrice = detailDict["total_price"]
                    {
                        self.lblTotalPrice.text = "$ \(getTotalPrice) + Free Shipping"
                    }

                    if  let getTotalSize = detailDict["total_upload_size"]
                    {
                        self.lblTotalSize.text = "Total Size : \(getTotalSize)"

                    }
                }
               else
                {

                let alertViewController = UIAlertController(title: NSLocalizedString("Alert!", comment: ""), message:"Something Went wrong please try again." , preferredStyle: .alert)
                let okAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default) { (action) -> Void in

                }
                alertViewController.addAction(okAction)
                self.present(alertViewController, animated: true, completion: nil)


        }
            }

        case .failure(let encodingError):
            print(encodingError)
        }
    }

The only one working for me at this date:

let headers: HTTPHeaders = [
            /* "Authorization": "your_access_token",  in case you need authorization header */
            "Content-type": "multipart/form-data"
        ]


            AF.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "file.jpeg", mimeType: "image/jpeg")
            },
                to: "http://35.227.31.145/new.php", method: .post , headers: headers)
                .response { resp in
                    print(resp)


            }

Updated code to Swift 5.

In swift 5 there is a change in this line

let imageData = UIImageJPEGRepresentation(image!, 0.2)!

To like this

let imageData = image.jpegData(compressionQuality: 0.50)

Whole code to upload image

let param: [String:Any] = ["your_parameters"]
var image = UIImage()
image = UIImage(named: "edit.png")!
let imageData = image.jpegData(compressionQuality: 0.50)
print(image, imageData!)

Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(imageData!, withName: "file", fileName: "swift_file.png", mimeType: "image/png")
        for (key, value) in param {
            multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
        }
    }, to: "your_url")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
                print("uploading \(progress)")

            })

            upload.responseJSON { response in
                //print response.result

            }
        case .failure( _): break
            //print encodingError.description
        }
    }
}
 user_photo is key for dic 
 swift_file.jpg is value for value  
 Write the same  withName is  key
 Write the same fileName is value 
 call the UploadImage(Image)


func UploadImage(img:UIImage) {
        let urlfinal = “ananda.profile.php";
        let parameters = ["user_id":"531", "user_photo”: "swift_file.jpg"] 
        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(img, 1)!, withName: "user_photo", fileName: "swift_file.jpeg", mimeType: "image/jpg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            print(multipartFormData)
        }, to:urlfinal)
        { (result) in
            switch result {
            case .success(let upload, , ):
                upload.uploadProgress(closure: { (progress) in
                })

                upload.responseJSON { response in
                    print(response)
                }
            case .failure( _): break

            }
        }
   }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!