Guest User

Untitled

a guest
Dec 5th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. NSURL *mediaURL=[info objectForKey:UIImagePickerControllerMediaURL];
  2. NSData *videoData=[NSData dataWithContentsOfURL:_videoURL];
  3. NSString *moviePath=[mediaURL path];
  4. SCRFTPRequest *ftpRequest=[[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://xyz.ca/"] toUploadFile:[mediaURL path]];
  5. ftpRequest.username = @"xyz.ca";
  6. ftpRequest.password = @"buKMH3ko8Nn";
  7.  
  8. //Specify a custom upload file name (optional)
  9. ftpRequest.customUploadFileName = @"h.MOV";
  10. //The delegate must implement the SCRFTPRequestDelegate protocol
  11. ftpRequest.delegate = self;
  12. [ftpRequest startRequest];
  13.  
  14. - (NSData *)generatePostDataForData:(NSData *)uploadData
  15. {
  16. // Generate the post header:
  17. NSString *post = [NSString stringWithCString:"--AaB03xrnContent-Disposition: form-data; name="upload[file]"; filename="somefile"rnContent-Type: application/octet-streamrnContent-Transfer-Encoding: binaryrnrn" encoding:NSASCIIStringEncoding];
  18.  
  19. // Get the post header int ASCII format:
  20. NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  21.  
  22. // Generate the mutable data variable:
  23. NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
  24. [postData setData:postHeaderData];
  25.  
  26. // Add the image:
  27. [postData appendData:uploadData];
  28.  
  29. // Add the closing boundary:
  30. [postData appendData:[@"rn--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
  31.  
  32. // Return the post data:
  33. return postData;
  34. }
  35.  
  36.  
  37. // For post video use this function
  38.  
  39. - (void)post:(NSData *)fileData
  40. {
  41. NSLog(@"POSTING");
  42.  
  43. // Generate the postdata:
  44. NSData *postData = [self generatePostDataForData: fileData];
  45. NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
  46.  
  47. // Setup the request:
  48. NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com:3000/"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30] autorelease];
  49. [uploadRequest setHTTPMethod:@"POST"];
  50. [uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
  51. [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
  52. [uploadRequest setHTTPBody:postData];
  53.  
  54. // Execute the request:
  55. NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
  56. if (conn)
  57. {
  58. // Connection succeeded (even if a 404 or other non-200 range was returned).
  59. NSLog(@"success");
  60. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  61. [alert show];
  62. [alert release];
  63. }
  64. else
  65. {
  66. // Connection failed (cannot reach server).
  67. NSLog(@"fail");
  68. }
  69. }
Add Comment
Please, Sign In to add comment