AFNetworking 파일업로드
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:MINWON_INSERT_URL]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html", @"application/json"]];
NSData *imageData = UIImageJPEGRepresentation(uploadImg, 0.5);
NSDictionary *parameters = @{@"user_name": [appD base64:appD.userNm], @"user_hp" : [appD base64:appD.userHp], @"comment" : inputTxt.text, @"latitude" : [NSString stringWithFormat:@"%f", putDLatitude], @"longitude" : [NSString stringWithFormat:@"%f", putDLongitude], @"location" : inputAddress.text };
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:@"append" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
NSError *error = nil;
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if([[resultDic objectForKey:@"code"] isEqualToString:@"1000"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림" message:@"민원등록이 되었습니다." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert show];
[appD menuAction2:1];
}else if([[resultDic objectForKey:@"code"] isEqualToString:@"1001"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림" message:@"필수데이터가 누락되었습니다." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert show];
}else if([[resultDic objectForKey:@"code"] isEqualToString:@"1002"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림" message:@"데이터베이스 오류, 관리자에게 문의하세요." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert show];
}else if([[resultDic objectForKey:@"code"] isEqualToString:@"1003"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림" message:@"파일업로드가 실패하였습니다." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
[alert show];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];