Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire 4 upload with parameters

I'm doing the below to upload a PNG file with parameters:

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

            // Send parameters
            multipartFormData.append((UserDefaults.standard.value(forKey: Email) as! String).data(using: .utf8)!, withName: "email")
            multipartFormData.append("png".data(using: .utf8)!, withName: "type")

        },
        to: "user/picture",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint("SUCCESS RESPONSE: \(response)")
                }
            case .failure(let encodingError):
                self.removeSpinnerFromView()
                print("ERROR RESPONSE: \(encodingError)")

            }
        }
    )

Problem is that on my server I don't see the email and type form fields. I followed examples posted online for this. Is there anything I should do differently for this?

EDIT

If I remove the part where I put:

multipartFormData.append(UIImagePNGRepresentation(tempImage!)!, withName: "file", fileName: "picture.png", mimeType: "image/png")

THEN the parameters are included. Otherwise not, I think this is a bug in Alamofire 4.0.1.

like image 748
KVISH Avatar asked Oct 01 '16 18:10

KVISH


People also ask

How do I upload files 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. almost right, uploadData. appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.

How do I upload multiple images in Swift?

Swift 3 Just use "[]" with image upload param to make it array of images. The above code is to upload multiple images collection, according to the code you have images Data (Foundation. Data) in imagesData array.

What is multipart form data in Swift?

multipart/form-data: Each value is sent as a block of data (“body part”), with a user agent-defined delimiter (“boundary”) separating each part. The keys are given in the Content-Disposition header of each part. This approach allows composite data types to be sent over http.


2 Answers

Its working fine on my side.

I'm using following code:

let parameters = [
            "file_name": "swift_file.jpeg"
        ]

Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
            }, to:"http://sample.com/upload_img.php")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
            })

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

        case .failure(let encodingError):
               //print encodingError.description
        }
    }
like image 171
Ekta Padaliya Avatar answered Dec 04 '22 21:12

Ekta Padaliya


If your value is of type Any, you can change it like this

for (key, value) in params {
    let paramsData:Data = NSKeyedArchiver.archivedData(withRootObject: value)
    formData.append(paramsData, withName: key)
}
like image 37
Yun Avatar answered Dec 04 '22 21:12

Yun