2016-10-05 4 views
0

cocos2dx에서 .png 파일을 PHP 서버로 업로드해야합니다. 내가 여기에 설명 된 같은 엔진을 편집cocos2dx에서 png 파일을 PHP 서버에 업로드하는 방법

(.txt를 테스트 목적으로 괜찮습니다) : 여기

http://discuss.cocos2d-x.org/t/upload-file-with-httpclient-solved/18028/4 과 : https://github.com/FenneX/FenneX/commit/134e9433c1dbc3ca6f772ce4c149bf911275a7e9

그래서 지금 무엇? 파일을 서버에 업로드하려면 어떻게해야합니까?

이것은 나의 시도이지만 나는 http/network noob이기 때문에 내가 무엇을하고 있는지 전혀 모른다 ... 나는 단지 간단한 예제 만 있으면된다.

cocos2dx 업로드 버튼 소스 :

// HTTP post 
    auto buttonPost = CustomButton::create("Post data",[this](Ref *pSender){ 

     __String *dataToSend = __String::create("dataOne=45&dataTwo=100"); 


     cocos2d::network::HttpRequest *request = 
      new cocos2d::network::HttpRequest(); 

     request->setUrl("http://XXX.XXX.XXX.XXX/post.php"); 
     request->setRequestType(cocos2d::network::HttpRequest::Type::POSTFILE); 

     request->setRequestData(dataToSend->getCString(), dataToSend->length()); 

     request->setFilePath("res/test.txt"); 

     request->setResponseCallback([=] 
      (network::HttpClient* client, 
      network::HttpResponse* response) 
     { 

      std::vector<char> *buffer = response->getResponseData(); 
      printf("Get data from server"); 

      for (unsigned int i = 0; i < buffer->size(); i++) 
      { 
       printf("%c", (*buffer)[i]); 
      } 

      printf("\n\n\n"); 

      printf("Response Code %li ", response->getResponseCode()); 

      if (200 == response->getResponseCode()) 
      { 
       printf("OK \n"); 
      } 
      else 
      { 
       printf("failed \n"); 
      } 

     }); 

     cocos2d::network::HttpClient::getInstance()->send(request); 
     request->release(); 

    }); 

PHP는 서버 측 코드 :

 <?php 
     $uploaddir = "uploads/"; 
     $uploadfile = $uploaddir . basename($_FILES['file']['name']); 

     if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) 
     { 
      echo "The file has been uploaded successfully"; 
     } 
     else 
     { 
      echo "There was an error uploading the file"; 
     } 
     ?> 

답변

0

덕분에이 '좋은 대답'에 대한 스택 오버 플로우 ...

클라이언트 코드 :

 //The url where you have the server side upload script 
     std::string url = "http://XXX.XXX.XXX.XXX/upload.php"; 


     //The basename is the whatever you use in the server side as 'file' id 
     //I think can be 'uploadedfile' 
     //or whatever as long as it'll be the same in both sides (client/server) 
     //in php: 
     //$_FILES['file']['name'] 
     //$_FILES['file']['tmp_name'] 

     std::string basename = "mobileFile"; 

     //File name and full path 
     std::string filename = "chart.png"; 
     std::string path = "res/" + filename; 

     long bufferSize = 0; 
     unsigned char* pBuffer = CCFileUtils::getInstance()->getFileData(path.c_str(), "r", &bufferSize); 

     //The same file stored as a string of "bytes" (it may contain zeroes) 
     std::string data = std::string((const char*)pBuffer, bufferSize); 


     //Create an http post request 
     cocos2d::network::HttpRequest *request = new cocos2d::network::HttpRequest(); 
     request->setUrl(url.c_str()); 
     request->setRequestType(cocos2d::network::HttpRequest::Type::POST); 

     //Include this boundary in the header and in the body of the request 
     std::string boundary = "---------------------------14737809831466499882746641449"; 

     //The content of the request must be multipart/form-data 
     std::vector<std::string> contentType; 
     contentType.push_back("Content-Type: multipart/form-data; boundary=" + boundary); 

     //Set the header with the previous content type 
     request->setHeaders(contentType); 

     //Build the body of the request. Include the boundary, basename and file name. 
     //Specify the content disposition and type 

     std::string body = "\r\n--" + boundary + "\r\n"; 
     body = body + "Content-Disposition: form-data; name=\"" + basename 
        + "\"; filename=\"" + filename + "\"\r\n"; 
     body = body + "Content-Type: application/octet-stream\r\n\r\n"; 

     //Then append the file data and again the boundary 
     body = body + data; 
     body = body + "\r\n--" + boundary + "--\r\n"; 

     request->setRequestData(body.data(), body.size()); 

     //Just a tag... 
     request->setTag("UPLOAD FILE"); 

     //Check that everything went OK when the request response calls your app back: 
      request->setResponseCallback([=] 
       (network::HttpClient* client, 
       network::HttpResponse* response) 
      { 

       std::vector<char> *buffer = response->getResponseData(); 
       printf("Get data from server:\n"); 

       for (unsigned int i = 0; i < buffer->size(); i++) 
        { printf("%c", (*buffer)[i]); } 

       printf(" Response Code %li ", response->getResponseCode()); 
       if (200 == response->getResponseCode()){ printf("OK \n"); } 
       else{ printf("failed \n"); } 

      }); 

     //Finally send the request: 
     cocos2d::network::HttpClient::getInstance()->send(request); 

     //And then get rid of it: 
     request->release(); 

PHP 코드 : (물론 이것은 테스트 용입니다)

<?php 

     $base = 'mobileFile'; 
     $uploaddir = 'uploads/'; 
     $file = basename($_FILES[$base]['name']); 
     $uploadfile = $uploaddir . $file; 

     echo "file=".$file; 

     if (move_uploaded_file($_FILES[$base]['tmp_name'], $uploadfile)) 
     { 
      echo " uploaded: ".$file; 
     } 
     else 
     { 
      echo "error"; 
     } 

    ?> 
관련 문제