0

I'm using this code (Swift) to upload an image that user selected from their photos to a server:

let imageFormatted = UIImageJPEGRepresentation(imageView.image!, 0.5);
    let uuid = NSUUID().UUIDString
    print ("MARK -- UUID is " + uuid)
    Alamofire.upload(
        .POST,
        "http://www.memer.onlie/upload.php",
        multipartFormData: { multipartFormData in
            multipartFormData.appendBodyPart(data: imageFormatted!, name: "imagefile",
                fileName: uuid + ".jpg", mimeType: "image/jpeg")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.validate()
                upload.responseJSON { response in
                    dispatch_async(dispatch_get_main_queue()) {
                        self.displayAlert("Uploaded!", message: "Your meme was uploaded, and you might see it in the app soon!", responseButtonText: "<3")
                    }
                    var json = JSON(data: response.data!)
                    print ("MARK -- JSON response: " + json["response"].stringValue)
                }
                print ("MARK -- Upload success!")
            case .Failure(let encodingError):
                print(encodingError)
                print ("MARK -- Upload failure!")
                self.displayAlert("Issue uploading.", message: "There was an issue uploading your meme.", responseButtonText: "Aww :(")
            }
        }
    )

No image is being uploaded to the server. What can I correct to get this to work?

Edited code.

1 Answer 1

0

This thread helps you understand what you have not considered and what you need to do to solve the issue. I guess you need to set request header and body part properly. If you use Alamofire and have to use 'multipart/form-data' encoding type, you can write code like this.

    Alamofire.upload(.POST, destURL, headers: yourHeader, multipartFormData: {    multipartFormData in

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

        // Append parameters you should send  
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }

        }, encodingCompletion: {  encodingResult in

            switch encodingResult {

            case .Success(let upload, _, _):
                upload.validate()
                upload.responseJSON { response in

                // do something if response is success
                }

            case .Failure(_):
                // do something if response is fail
            }
    })
Sign up to request clarification or add additional context in comments.

2 Comments

I'm using this, see the edited post for the code, but it's not sending to the server and there isn't a response. @woogii
Don't you have any HTTP header or extra parameters? What's the error you get from the response? You can refer to this thread where it shows various example codes about Alamorfire Upload request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.