2011-12-19 2 views
2

내 앱에서 UIImagePickerController을 통해 선택한 이미지를 JPEG 이미지 만 허용하는 데이터베이스로 업로드하고 싶습니다. 나는 여기에서 많은 질문을 봤는데, 다른 포럼에서는 여전히 작동하지 않았다. 내가 볼 수없는 실수가 있다면 내 코드를 확인할 수 있기를 바랍니다. 문서는 이미지에 대한 이미지 정보, 화상 제목 및 위치 데이터와, 업로드 용 완전한 방법이다 :는 UIImageMultipart/Formdata 이미지 업로드,

- (void) uploadPic{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *destinationFilePath = [[NSString alloc] initWithFormat: @"%@/%@.jpeg", documentsDirectory, self.chosenImage.imgTitle]; 
    NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath]; 
    NSLog(@"will upload file: %@", destinationFilePath); 

    NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL]; (*1) 
    NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90); (*2) 
    //Here i tried 2 ways of getting the data for uploading, but both don't work. 

    // create the URL 
    NSURL *postURL = [NSURL URLWithString:@"http://*********/PictureUpload"]; 

    // create the connection 
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; 

    // change type to POST (default is GET) 
    [postRequest setHTTPMethod:@"POST"]; 

    // just some random text that will never occur in the body 
    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 

    // header value, user session ID added 
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", 
           sessionID]; 

    // set header 
    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 
    // create data 
    NSMutableData *postBody = [NSMutableData data]; 

    // title part 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[self.chosenImage.imgTitle dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    // desc part 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[self.chosenImage.imgDescription dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


    // latitude part 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[self.chosenImage.latitude dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    // longitude part 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[self.chosenImage.longitude dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    // media part 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[NSData dataWithData:imageURLdata]]; 
    [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    // final boundary 
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding]; 
    NSLog(@"%@", s); 

    // add body to post 
    [postRequest setHTTPBody:postBody]; 

    // pointers to some necessary objects 
    NSURLResponse* response; 
    NSError* error; 

    // synchronous filling of data from HTTP POST response 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error]; 

    if (error) 
    { 
     NSLog(@"Error: %@", [error localizedDescription]); 
    } 

    // convert data into string 
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] 
                 length:[responseData length] 
                 encoding:NSUTF8StringEncoding]; 

    // see if we get a welcome result 
    NSLog(@"%@", responseString); 
    [self responseHandler:responseString]; 

} 

GEOimagechosenImageCGImageRef 통해 생성은 ImagePickerController에서 선택된다. 방법 nr. 업로드에 NSData을 얻으려면 * 1이 좋지 않습니다. 여기서는 이미지가 문서 디렉토리에서 선택되고 여기에 이미지에 대한 모든 EXIF ​​정보가 삭제되기 때문입니다. 두 가지 방법으로 이미지 파일 형식이 지원되지 않는 (JPEG 예상) 데이터베이스의 응답을받습니다. 메소드 nr로 생각했습니다. * 2 JPEG 이미지를 보내고 있지만 아마도 multipart/formdata 프로세스 전체에서 실수를 저질렀을 것입니다.

파일 시스템의 원본 파일에 대한 URL을 가져 오려고했지만 매우 어려웠습니다. 이미지에서 자산 URL 만 가져 왔습니다. 사전에

덕분에

S4lfish

답변

2

당신은 아마 지금 쯤은 스스로 그것을 해결했습니다,하지만 난 당신의 문제를 볼 생각합니다.

NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 

// header value, user session ID added 
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", 
          sessionID]; 

MIME 헤더 내에 경계를 정의 할 때 세션 ID를 경계로 사용하고 있습니다. 그런 다음 아래에서 실제 변수를 만들기 위해 변수 stringBoundary을 사용하고 있습니다.

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

는 또한 처리 코드를 돕기 위해 각 MIME 블록의 콘텐츠 길이를 제공하는 좋은 생각이 될 수도 있지만 나는 여기에서 문제로 생각하지 않습니다.

0

는 >>> ----이 시도 //

NSMutableDictionary *post_dic=[[NSMutableDictionary 
    alloc]initWithCapacity:20]; [post_dic setObject:firstName_txtFld.text 
    forKey:@"firstname"]; [post_dic setObject:lastName_txtFld.text 
    forKey:@"lastname"]; [post_dic setObject:email_txtFld.text 
    forKey:@"email"]; [post_dic setObject:password_txtFld.text 
    forKey:@"password"]; [post_dic setObject:country_txtFld.text 
    forKey:@"address"]; [post_dic setObject:state_txtFld.text 
    forKey:@"state"]; [post_dic setObject:city_txtFld.text 
    forKey:@"city"]; [post_dic setObject:zip_txtFld.text forKey:@"zip"]; 
    [post_dic setObject:phoneNumber_txtFld.text forKey:@"phonenumber"]; 
    NSURL *url = [NSURL URLWithString:@"Enter your url"]; 
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest 
    requestWithURL:url]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest 
    setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]]; 
      NSString *boundary = @"--------------------------14737809831466499882746641449"; NSString 
    *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [urlRequest addValue:contentType 
    forHTTPHeaderField: @"Content-Type"]; 
      NSMutableData *postbody = [NSMutableData data]; 
      for (NSString *param in post_dic) { 
     [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; 
    name=\"%@\"\r\n\r\n", param] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:[[NSString stringWithFormat:@"%@\r\n", [post_dic objectForKey:param]] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
      } 
     [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; 
    name=\"filedata\"; 
    filename=\"%@.jpg\"\r\n",@"text"] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     //NSLog(@" image -->%@",imageData); 
     [postbody appendData:[NSData dataWithData:imageData]]; 
     [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] 
    dataUsingEncoding:NSUTF8StringEncoding]]; 
     [urlRequest setHTTPBody:postbody]; [urlRequest setURL:url]; 
     NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
[connection start];