2013-07-08 1 views
0

오늘 아침에 iPhone으로 찍은 사진을 blobstore에 업로드하는 방법에 대한 분명한 예제를 찾고 있지만 성공하지는 못했습니다. 현재iphone에서 GAE blobstore로 이미지 업로드

나는 서버에서이 코드로, PHP의 서버에 사진을 보낼 수있는, 내 아이폰 앱 개발이 :

이 작동하게 목표 - C의 부분 (I '
// Function to upload a photo in a file and save data in the DB 
function upload($photoData, $descr, $phone) { 
    // Folder to upload data 
    $path = $_SERVER['DOCUMENT_ROOT']."/program/data/"; 

    // Check if there was no error during the file upload 
    if ($photoData['error'] == 0) { 
     $result = query("INSERT INTO pics(descr, phone) VALUES('%s','%s')", $descr, $phone); 
     if (!$result['error']) { 
      // Inserted in the database, go on with file storage 
      // Obtain database link (in lib.php) 
      global $link; 

      // Get the last automatically generated ID 
      $idPhoto = mysqli_insert_id($link); 

      // Move the temporarily stored file to a convenient location 
      if (move_uploaded_file($photoData['tmp_name'], $path.$idPhoto.".jpg")) { 
       // File moved, all good, generate thumbnail 
       thumb($path.$idPhoto.".jpg", 180); 
       print json_encode(array('successful' => 1)); 
      } else { 
       errorJson('Upload on server problem'); 
      } 
     } else { 
      errorJson('Save database problem: '.$result['error']); 
     } 
    } else { 
     errorJson('Upload malfunction.'); 
    } 
} 

입니다 AFNetworking을 사용하고 있습니다 및 객체 API의 sharedInstance)는 AFJSONRequestOperation 클래스입니다 :

// Upload the image and the description to the web service 
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys: 
              @"upload", @"command", 
              UIImageJPEGRepresentation(originalPhoto, 70), @"file", 
              description, @"descr", 
              phoneNumber, @"phone", 
              nil] 
           onCompletion:^(NSDictionary *json) { 
            // Finished and response from server 
            if (![json objectForKey:@"error"]) { 
             // Success 
             [[[UIAlertView alloc]initWithTitle:@"Info" 
                    message:@"Thanks" 
                   delegate:nil 
                 cancelButtonTitle:@"Dismiss" 
                 otherButtonTitles: nil] show]; 

             // Send a notification so the main view can reload the data 
             [[NSNotificationCenter defaultCenter] postNotificationName:@"updateStream" object:nil]; 
            } else { 
             // Error 
             NSString* errorMsg = [json objectForKey:@"error"]; 
             [UIAlertView error:errorMsg]; 
            } 
           }]; 

이 잘 작동하고 이미지가 서버에 저장됩니다. 하지만 파일을 저장할 수없는 데이터 저장소와 동일한 것을 만들고 싶습니다. 그래서 나는 이미지 저장에 대해 연습 할 웹 페이지를 만들었고, 표준 웹 폼의 블롭 스토어에 문제없이 이미지를 업로드 할 수 있습니다.

# Get and post for the create page 
class Create(PicturePageHandler, blobstore_handlers.BlobstoreUploadHandler): 
    def get(self): 
     if self.user_logged_in(): 
      # The session for upload a file must be new every reload page 
      uploadUrl = blobstore.create_upload_url('/addPic') 
      self.render_page("addPicture.htm", form_action=uploadUrl) 

    def post(self): 
     if self.user_logged_in(): 
      # Create a dictionary with the values, we will need in case of error 
      templateValues = self.template_from_request() 
      # Test if all data form is valid 
      testErrors = check_fields(self) 

      if testErrors[0]: 
       # No errors, save the object 
       try: 
        # Get the file and upload it 
        uploadFiles = self.get_uploads('picture') 
        # Get the key returned from blobstore, for the first element 
        blobInfo = uploadFiles[0] 
        # Add the key and the permanent url to the template 
        templateValues['blobKey'] = blobInfo.key() 
        templateValues['servingUrl'] = images.get_serving_url(blobInfo.key(), size=None) 

        # Save all 
        pic = Picture.save(self.user.key, **templateValues) 
        if pic is None: 
         logging.error('Picture save error.') 

        self.redirect("/myPics") 

       except: 
        self.render_page("customMessage.htm", custom_msg=_("Problems while uploading the picture.")) 
      else: 
       # Errors, render the page again, with the values, and showing the errors 
       templateValues = custom.prepare_errors(templateValues, testErrors[1]) 
       # The session for upload a file must be new every reload page 
       templateValues['form_action'] = blobstore.create_upload_url('/addPic') 

       self.render_page("addPicture.htm", **templateValues) 

내 질문

은 다음과 같습니다 : 이것은 내가 GAE에 저장하기 위해 사용하고 코드입니다 (PicturePageHandler 또는 render_page처럼 내 자신의 헬퍼 클래스 나 함수에 대해 잊지)

  1. 것은 아직도 사용 할 수 있습니까 Objective-C JSON 호출을 사용하여 그림을 서버에 업로드하거나 그림 업로드 방법을 완전히 변경해야합니까?
  2. 가능한 경우 JSON에서 사진을 가져 오기 위해 Python 서버 코드를 어떻게 바꿀 수 있습니까?
+0

어떤 아이디어? 누구든지 도울 수 있니? 더 자세한 정보가 필요하십니까? – Eagle

답변