Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created July 23, 2012 03:26
Show Gist options
  • Save tatsuro-ueda/3161878 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/3161878 to your computer and use it in GitHub Desktop.
Objectice-Cでファイルをアップロードする
- (IBAction)upload:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://192.168.100.203:4567/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:_filePath.lastPathComponent], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:_filePath.lastPathComponent mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// [operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
// NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
// }];
[operation start];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment