Created
May 11, 2009 10:00
-
-
Save paulanthonywilson/109937 to your computer and use it in GitHub Desktop.
Hack multipart uploader - from spike.
This file contains 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
#import "RecordingUploader.h" | |
#import "RestConfiguration.h" | |
@implementation RecordingUploader | |
-(void)uploadFile:(NSString*)file{ | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d/recordings", [RestConfiguration host], [RestConfiguration port]]]; | |
NSData *data = [NSData dataWithContentsOfFile:file]; | |
NSString *stringBoundary = [NSString stringWithString:@"0xEumolpusBoundary"]; | |
NSMutableDictionary *headers = [[[NSMutableDictionary alloc] init] autorelease]; | |
[headers setValue:@"no-cache" forKey:@"Cache-Control"]; | |
[headers setValue:@"no-cache" forKey:@"Pragma"]; | |
[headers setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary] | |
forKey:@"Content-Type"]; | |
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url | |
cachePolicy:NSURLRequestUseProtocolCachePolicy | |
timeoutInterval:60.0]; | |
[request setHTTPMethod:@"POST"]; | |
[request setAllHTTPHeaderFields:headers]; | |
NSMutableData *postData = [NSMutableData dataWithCapacity:[data length] + 512]; | |
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] | |
dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postData appendData:[@"Content-Type: application/x-wav\r\n" | |
dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postData appendData:[@"Content-Disposition: form-data; name=\"recording[recording]\"; filename=\"recording.wav\"\r\n\r\n" | |
dataUsingEncoding:NSUTF8StringEncoding]]; | |
[postData appendData:data]; | |
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] | |
dataUsingEncoding:NSUTF8StringEncoding]]; | |
[request setHTTPBody:postData]; | |
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request | |
delegate:self | |
startImmediately:YES]; | |
NSLog(@"connection: %@", conn); | |
if (conn){ | |
[conn release]; | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ | |
NSLog(@"didReceiveData"); | |
} | |
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ | |
NSLog(@"didReceiveResponse: %@", response); | |
} | |
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
NSLog(@"connectionDidFinishLoading"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment