Advertisement
Guest User

Untitled

a guest
Jul 28th, 2012
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. iOS 5 - AFNetworking - Uploading Video
  2. NSString *vidURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mov"];
  3. NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:vidURL]];
  4.  
  5. NSLog(@"The test vid's url is %@.",vidURL);
  6.  
  7. AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL fileURLWithPath: @"http://www.mywebsite.com"]];
  8.  
  9. NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
  10. {
  11. [formData appendPartWithFileData:videoData name:@"file" fileName:@"test.mov" mimeType:@"video/quicktime"];
  12. }];
  13.  
  14. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
  15.  
  16. [operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
  17. {
  18.  
  19. NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
  20.  
  21. }];
  22.  
  23. [operation start];
  24.  
  25. [operation start];
  26.  
  27. [httpClient enqueueHTTPRequestOperation:operation];
  28.  
  29. httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.mysite.com"]];
  30.  
  31. NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
  32. {
  33. [formData appendPartWithFileData:videoData name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime"];
  34. }];
  35.  
  36.  
  37. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
  38.  
  39. [operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
  40. {
  41.  
  42. NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
  43.  
  44. }];
  45.  
  46. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Success");}
  47. failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@", operation.responseString);}];
  48. [operation start];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement