Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2.0 track file upload progress

I am relatively new to AFNetworking 2.0. Using the code snippet below, I've been able to successfully upload a photo to my url. I would like to track the incremental upload progress, but I cannot find an example of doing this with version 2.0. My application is iOS 7, so I've opted for AFHTTPSessionManager.

Can anyone offer an example of how to modify this snippet to track upload progress?

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"myimage.jpg"], 1.0);  [manager POST:@"http://myurl.com" parameters:dataToPost constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {     [formData appendPartWithFileData:imageData name:@"attachment" fileName:@"myimage.jpg" mimeType:@"image/jpeg"]; } success:^(NSURLSessionDataTask *task, id responseObject) {     NSLog(@"Success %@", responseObject); } failure:^(NSURLSessionDataTask *task, NSError *error) {     NSLog(@"Failure %@, %@", error, [task.response description]); }]; 
like image 315
Benchtop Creative Avatar asked Dec 12 '13 02:12

Benchtop Creative


1 Answers

The interface of AFHTTPSession doesn't provide a method to set a progress block. Instead, you'll have to do the following:

// 1. Create `AFHTTPRequestSerializer` which will create your request. AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];  // 2. Create an `NSMutableURLRequest`. NSMutableURLRequest *request =     [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://www.myurl.com"                                     parameters:dataToPost                      constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {                        [formData appendPartWithFileData:imageData                                                    name:@"attachment"                                                fileName:@"myimage.jpg"                                                mimeType:@"image/jpeg"];                      }];  // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *operation =     [manager HTTPRequestOperationWithRequest:request                                      success:^(AFHTTPRequestOperation *operation, id responseObject) {                                        NSLog(@"Success %@", responseObject);                                      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                                        NSLog(@"Failure %@", error.description);                                      }];  // 4. Set the progress block of the operation. [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,                                     long long totalBytesWritten,                                     long long totalBytesExpectedToWrite) {   NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); }];  // 5. Begin! [operation start]; 

In addition, you don't have to read the image via UIImage and then compress it again using JPEG to get an NSData. Just use +[NSData dataWithContentsOfFile:] to read the file directly from your bundle.

like image 140
StatusReport Avatar answered Oct 09 '22 13:10

StatusReport