Uploading file with parameters using Alamofire Uploading file with parameters using Alamofire ios ios

Uploading file with parameters using Alamofire


Here is a simple function that requires the target upload url, parameters, and imageData and returns the URLRequestConvertible and NSData that Alamofire.upload requires to upload an image with parameters.

// this function creates the required URLRequestConvertible and NSData we need to use Alamofire.uploadfunc urlRequestWithComponents(urlString:String, parameters:Dictionary<String, String>, imageData:NSData) -> (URLRequestConvertible, NSData) {    // create url request to send    var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)    mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue    let boundaryConstant = "myRandomBoundary12345";    let contentType = "multipart/form-data;boundary="+boundaryConstant    mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")    // create upload data to send    let uploadData = NSMutableData()    // add image    uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)    uploadData.appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)    uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)    uploadData.appendData(imageData)    // add parameters    for (key, value) in parameters {        uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)        uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)    }    uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)    // return URLRequestConvertible and NSData    return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)}    

Here's an example of how to use it (see CREATE AND SEND REQUEST):

// init paramters Dictionaryvar parameters = [    "task": "task",    "variable1": "var"]// add addtionial parametersparameters["userId"] = "27"parameters["body"] = "This is the body text."// example image datalet image = UIImage(named: "177143.jpg")let imageData = UIImagePNGRepresentation(image)// CREATE AND SEND REQUEST ----------let urlRequest = urlRequestWithComponents("http://example.com/uploadText/", parameters: parameters, imageData: imageData)Alamofire.upload(urlRequest.0, urlRequest.1)    .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in        println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")    }    .responseJSON { (request, response, JSON, error) in        println("REQUEST \(request)")        println("RESPONSE \(response)")        println("JSON \(JSON)")        println("ERROR \(error)")}    

And if you need the php file for the target url (with an 'uploads' folder in the same directory):

// get picture variables$file       = $_FILES['file']['tmp_name'];$fileName   = $_FILES['file']['name'];$fileType   = $_FILES['file']['type'];// check extension$allowedExts = array("jpg", "jpeg", "png");$rootName = reset(explode(".", $fileName));$extension = end(explode(".", $fileName));// create new file name$time = time();$newName = $rootName.$time.'.'.$extension;// temporarily save file$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$newName );if ($moved) $path = "uploads/".$newName;$body = $_POST['body'];$userId = $_POST['userId'];$time = time();if ($moved) {    $fullUrl = "http://antiblank.com/testPhotoUpload/".$path;    $arrayToSend = array('status'=>'success','time'=>$time,'body'=>$body,'userId'=>$userId, "imageURL"=>$fullUrl);} else {    $arrayToSend = array('status'=>'FAILED','time'=>$time,'body'=>$body,'userId'=>$userId);}header('Content-Type:application/json');echo json_encode($arrayToSend);


Upload Photo / File with parameters and custom headers via Swift 3 & 4 and Alamofire 4

// import Alamofirefunc uploadWithAlamofire() {  let image = UIImage(named: "bodrum")!  // define parameters  let parameters = [    "hometown": "yalikavak",    "living": "istanbul"  ]  Alamofire.upload(multipartFormData: { multipartFormData in    if let imageData = UIImageJPEGRepresentation(image, 1) {      multipartFormData.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png")    }    for (key, value) in parameters {      multipartFormData.append((value?.data(using: .utf8))!, withName: key)    }}, to: "upload_url", method: .post, headers: ["Authorization": "auth_token"],        encodingCompletion: { encodingResult in          switch encodingResult {          case .success(let upload, _, _):            upload.response { [weak self] response in              guard let strongSelf = self else {                return              }              debugPrint(response)            }          case .failure(let encodingError):            print("error:\(encodingError)")          }  })}

via Swift 2 and Alamofire 3

  // import Alamofire  func uploadWithAlamofire() {    let image = UIImage(named: "myImage")!    // define parameters    let parameters = [      "hometown": "yalikavak",      "living": "istanbul"    ]    // Begin upload    Alamofire.upload(.POST, "upload_url",      // define your headers here      headers: ["Authorization": "auth_token"],      multipartFormData: { multipartFormData in        // import image to request        if let imageData = UIImageJPEGRepresentation(image, 1) {          multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png")        }        // import parameters        for (key, value) in parameters {          multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)        }      }, // you can customise Threshold if you wish. This is the alamofire's default value      encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,      encodingCompletion: { encodingResult in        switch encodingResult {        case .Success(let upload, _, _):          upload.responseJSON { response in            debugPrint(response)          }        case .Failure(let encodingError):          print(encodingError)        }    })  }

Current swift version:https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#uploading-data-to-a-server


Here is a Solution using Alamofire 3.0 based on antiblanks answer:

 let parameters = [            "par1": "value",            "par2": "value2"]     let URL = "YOUR_URL.php" let image = UIImage(named: "image.png") Alamofire.upload(.POST, URL, multipartFormData: {                multipartFormData in                if let _image = image {                    if let imageData = UIImageJPEGRepresentation(_image, 0.5) {                        multipartFormData.appendBodyPart(data: imageData, name: "file", 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, _, _):                     upload.responseObject { (response: Response<UploadData, NSError>) -> Void in                     switch response.result {                     case .Success:                         completionHandler?(success: true)                     case .Failure(let error):                         completionHandler?(success: false)                     }                 }                case .Failure(let encodingError):                    print(encodingError)                }        })