0
func uploadProfilePicture(data : NSData,completion: (success : Bool) -> Void) {

        PKHUD.sharedHUD.contentView = PKHUDTextView(text: "Loading...")
        PKHUD.sharedHUD.show()
        Alamofire.upload(.POST,GlobalConstants.KUpdateProPic,multipartFormData:
            {
                multipartFormData in
                multipartFormData.appendBodyPart(data : data, name: "image", fileName: Helper.timeStamp(), mimeType: "image/png")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)

                    let status : NSString = response.result.value?.valueForKey("status") as! String
                    if(status .isEqualToString("1")){
                        let profile_image : NSString = response.result.value?.valueForKey("profile_image") as! String
                        defaults.setObject(profile_image, forKey:"imageURL")
                        let fileUrl = NSURL(string: profile_image as String)
                         MyViewState.profile_image = (fileUrl?.lastPathComponent)!
                        PKHUD.sharedHUD.hide()
                        completion (success: true)
                    }else{
                         PKHUD.sharedHUD.hide()
                        completion(success : false)
                    }
                }
            case .Failure(let encodingError):
                 print(encodingError)
                 PKHUD.sharedHUD.hide()
                 completion(success : false)

            }        })
    }

Above is my code to upload image. Now, I want to send some other parameters with this upload and I want to know how many bytes are written.

I have tried to use the links below but I haven't been able to achieve this. Uploading file with parameters using Alamofire

Community
  • 1
  • 1
ZetrixWeb
  • 47
  • 1
  • 8

1 Answers1

0

As @anbu-karthik mentioned, you can add the additional keys by making additional calls to the multipartFormData object:

var value: Int = 10
let data = NSData(bytes: &value, length: sizeof(Int))

multipartFormData.appendBodyPart(data: data, name: "userid")

For computing the total encoded size, you can simply query the multipartFormData object for it's content size after you're done adding body parts to it:

print("Total Content Length: \(multipartFormData.contentLength)")

Then you'll need to figure out how you want to store that value by digging it out of the closure. But that should be trivial.

cnoon
  • 16,575
  • 7
  • 58
  • 66