Search code examples
phpc++file-uploaduploadcocos2d-x

Cocos2d-x - How to upload file to server using PHP?


I want to upload a file with POST method using CCHttpClient.

server has been all ready to receive the file because I use Java and Objective C already a successful upload.

I'm wondering how to make the POST data to the file in cocos2d-x.

std :: string path = CCFileUtils :: sharedFileUtils () -> getWritablePath () + "temp.png";
unsigned long buff = 0; 
unsigned char * pBuffer = CCFileUtils :: sharedFileUtils () -> getFileData (path.c_str (), "r", & buff); 

First made ​​in the form of unsigned char data file,

const char * fileBinary = (const char *) pBuffer; 

has a cast to const char format.

The following have made it to the POST data.

std :: string boundary = "--------------------------- 14737809831466499882746641449";
std :: string str = "\ r \ n--" + boundary + "\ r \ n"; 
str = str + "Content-Disposition: attachment; name = \" upload_file \ "; filename = \" 11.png \ "\ r \ n"; 
str = str + "Content-Type: application / octet-stream \ r \ n \ r \ n"; 
str = str + fileBinary; <- This is the data portion of the file. 
str = str + "\ r \ n--" + boundary + "- \ r \ n"; 

Do this by making a transfer to the POST method, the file is uploaded but uploaded file data has Just a 8 byte.

How do I create a data to send file?

Here is full code.

std::string path = CCFileUtils::sharedFileUtils()->getWritablePath() + "temp.png";
unsigned long buff = 0;
unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "r", &buff);

const char* fileBinary = (const char*)pBuffer;


std::string boundary = "---------------------------14737809831466499882746641449";
std::string bound = boundary;
std::vector<std::string> headers;
headers.push_back("Content-Type: multipart/form-data; boundary="+bound);

std::string str = "\r\n--" + boundary + "\r\n";
str = str + "Content-Disposition: attachment; name=\"upload_file\"; filename=\"11.png\"\r\n";
str = str + "Content-Type: application/octet-stream\r\n\r\n";
str = str + fileBinary;
str = str + "\r\n--" + boundary + "--\r\n";


CCHttpRequest* request = new CCHttpRequest();
request->setUrl("SERVER ADDRESS"); 
request->setHeaders(headers);
request->setRequestType(CCHttpRequest::kHttpPost);
request->setRequestData(str.c_str(), strlen(str.c_str()));
request->setResponseCallback(this, httpresponse_selector(MainScene::complete));
request->setTag("UP IMAGE");
CCHttpClient::getInstance()->send(request);
request->release();

Solution

  • The 9th byte of the PNG is zero.

    When you concatenate the file data here:

    str = str + fileBinary;
    

    it only gets copied up to the first zero.

    You should be able to construct a "binary" string like this:

     std::string(fileBinary, lengthOfFileBinary)
    

    as this constructor can create a string containing null bytes.
    You'll need to get the size from something other that strlen, since strlen is pointless with binary data.
    (Cocos probably has a function that returns a file size).

    Then you can use

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