Created
September 12, 2011 17:40
-
-
Save KarlHerler/1211871 to your computer and use it in GitHub Desktop.
fugly file uploading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TaskViewController.m | |
// RecPotato | |
// | |
// Created by Karl Herler on 8/9/11. | |
// Copyright 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "TaskViewController.h" | |
#import <MobileCoreServices/UTCoreTypes.h> | |
@implementation TaskViewController | |
@synthesize taskDescription, taskName, taskLocation, taskValue, doButton, videoPicker, receivedData, progessing; | |
// default actions | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark Actions | |
- (IBAction)doButtonPress { | |
//show the video picker | |
[self primeVideo]; | |
[self.navigationController presentModalViewController:self.videoPicker animated:YES]; | |
} | |
#pragma mark helper methods | |
- (void)primeVideo { | |
//initialize the video picker | |
self.videoPicker = [[UIImagePickerController alloc] init]; | |
//Do some configuration | |
self.videoPicker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
self.videoPicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil]; | |
self.videoPicker.delegate = self; | |
} | |
- (NSData *)generatePostDataForData:(NSData *)uploadData { | |
// method gotten from: http://stackoverflow.com/questions/1065628/uploading-video-with-iphone | |
NSMutableString *post_mutable = [NSMutableString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\"; filename=\"" encoding:NSASCIIStringEncoding]; | |
[post_mutable appendString:[taskName text]]; | |
[post_mutable appendString:[NSString stringWithCString: "\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding]]; | |
// Generate the post header: | |
NSString *post = post_mutable; | |
// Get the post header int ASCII format: | |
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; | |
// Generate the mutable data variable: | |
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length]]; | |
[postData setData:postHeaderData]; | |
// Add the image: | |
[postData appendData: uploadData]; | |
// Add the closing boundry: | |
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]]; | |
// Return the post data: | |
return postData; | |
} | |
- (void)post:(NSData *)fileData { | |
/* method gotten from: http://stackoverflow.com/questions/1065628/uploading-video-with-iphone | |
* Modified by Karl Herler on 19-08-2011. | |
*/ | |
NSLog(@"POSTING"); | |
NSURL *url = [NSURL URLWithString:@"http://46.51.175.244:1337/"]; | |
// Generate the postdata: | |
NSData *postData = [self generatePostDataForData: fileData]; | |
//NSString *postData = @"asdasdsa"; | |
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; | |
// Setup the request: | |
NSMutableURLRequest *uploadRequest; | |
uploadRequest = [[[NSMutableURLRequest alloc] initWithURL: url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 2000 ] autorelease]; | |
[uploadRequest setHTTPMethod:@"POST"]; | |
[uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; | |
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"]; | |
[uploadRequest setHTTPBody:postData]; | |
// Execute the reqest: | |
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self]; | |
//Boolean conn = 1; | |
if (conn) { | |
// Connection succeeded (even if a 404 or other non-200 range was returned). | |
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uploading" message:@"Upload in progress..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
[alert release];*/ | |
/* request in progress dialog */ | |
progessing = [[[UIAlertView alloc] initWithTitle:@"Uploading your video\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; | |
[progessing show]; | |
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; | |
// Adjust the indicator so it is up a few pixels from the bottom of the alert | |
indicator.center = CGPointMake(progessing.bounds.size.width / 2, progessing.bounds.size.height - 50); | |
[indicator startAnimating]; | |
[progessing addSubview:indicator]; | |
[indicator release]; | |
NSLog(@"sucess"); | |
} else { | |
// Connection failed (cannot reach server). | |
NSLog(@"fail"); | |
} | |
} | |
#pragma mark NSURLConnection methods | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
// This method is called when the server has determined that it | |
// has enough information to create the NSURLResponse. | |
// It can be called multiple times, for example in the case of a | |
// redirect, so each time we reset the data. | |
// receivedData is an instance variable declared elsewhere. | |
NSLog(@"didReceiveResponse called"); | |
[receivedData setLength:0]; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
// Append the new data to receivedData. | |
// receivedData is an instance variable declared elsewhere. | |
NSLog(@"didReceiveData called"); | |
[receivedData appendData:data]; | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
// do something with the data | |
// receivedData is declared as a method instance elsewhere | |
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); | |
[progessing dismissWithClickedButtonIndex:0 animated:YES]; | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload" message:@"Upload was successful." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
[alert release]; | |
// release the connection, and the data object | |
[connection release]; | |
[receivedData release]; | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
// release the connection, and the data object | |
[connection release]; | |
// receivedData is declared as a method instance elsewhere | |
[receivedData release]; | |
[progessing dismissWithClickedButtonIndex:0 animated:YES]; | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload" message:@"Upload failed\nSorry." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
[alert release]; | |
// inform the user | |
NSLog(@"Connection failed! Error - %@ %@", | |
[error localizedDescription], | |
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); | |
} | |
#pragma mark UIImagePickerController methods | |
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
//assign the mediatype to a string | |
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; | |
//check the media type string so we can determine if its a video | |
if ([mediaType isEqualToString:@"public.movie"]){ | |
NSLog(@"got a movie"); | |
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; | |
NSData *webData = [NSData dataWithContentsOfURL:videoURL]; | |
[self post:webData]; | |
//[webData release]; | |
} | |
[[picker parentViewController] dismissModalViewControllerAnimated:YES]; | |
[picker release]; | |
} | |
/*- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { | |
puts("canceled"); | |
}*/ | |
#pragma mark - View lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view from its nib. | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment