1: NSString *urlString = @"yourUrl";
2: NSURL* url = [NSURL URLWithString:urlString];
3:
4: AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
5: NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
6: NSURL *localVideoURL = [NSURL URLWithString:[userDefaults objectForKey:@"videoURL"]];
7: NSData *videoData = [NSData dataWithContentsOfURL:localVideoURL];
8:
9: NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
10: [formData appendPartWithFileData:videoData name:@"video_file" fileName:@"testvideo.mov" mimeType:@"video/quicktime"];
11: [formData appendPartWithFormData:[[BAUserInfoParser userInfoJson] dataUsingEncoding:NSUTF8StringEncoding] name:@"userInfo"];
12: [formData appendPartWithFormData:[[userDefaults objectForKey:@"transactionReceiptData"] dataUsingEncoding:NSUTF8StringEncoding] name:@"transactionData"];
13: }];
14:
15: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
16: [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
17: // NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
18: float uploadPercentge = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
19: float uploadActualPercentage = uploadPercentge *100;
20: [lblUploadInfoText setText:[NSString stringWithFormat:@"%.2f %%",uploadActualPercentage]];
21: if (uploadActualPercentage >= 100) {
22: lblStatus.text = @"Waitting for response ...";
23: }
24: progressBar.progress = uploadPercentge;
25: }];
26: [httpClient enqueueHTTPRequestOperation:operation];
27:
28: [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
29: lblStatus.text = @"Upload Complete";
30: NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
31: NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
32: }
33: failure:^(AFHTTPRequestOperation *operation, NSError *error) {
34: NSLog(@"error: %@", operation.responseString);
35: NSLog(@"%@",error);
36: }];
37: [operation start];
38: }
Swift 2.0
1: let imageData = UIImageJPEGRepresentation(self.myImage.image, 0.2)
2: let manager = AFHTTPRequestOperationManager(baseURL: nil)
3: manager.requestSerializer.setValue(self.accessToken, forHTTPHeaderField: "Authorization")
4: manager.requestSerializer.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
5: manager.responseSerializer = AFJSONResponseSerializer()
6: let operation = manager.POST(URL.PackageImageUploadURL, parameters: parameters, constructingBodyWithBlock: { formData in
7: formData.appendPartWithFileData(
8: imageData!,
9: name: "image",
10: fileName: "image.jpg",
11: mimeType: "image/jpeg")
12: }, success: { (operation, response) in
13: print(response)
15: }, failure: { (operation, error) in
16: // TODO: - Handle error case
17: print(error)
19: })
20: operation?.start()