c++ - Cocos2d-x - How to upload file to server using PHP? -
i want upload file post method using cchttpclient.
server has been ready receive file because utilize java , objective c successful upload.
i'm wondering how create post info 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 form of unsigned char info file,
const char * filebinary = (const char *) pbuffer;
has cast const char format.
the next have made 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; <- info portion of file. str = str + "\ r \ n--" + boundary + "- \ r \ n";
do making transfer post method, file uploaded uploaded file info has 8 byte.
how create info send file?
here total 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();
the 9th byte of png zero.
when concatenate file info here:
str = str + filebinary;
it gets copied first zero.
you should able build "binary" string this:
std::string(filebinary, lengthoffilebinary)
as constructor can create string containing null bytes. you'll need size other strlen
, since strlen
pointless binary data. (cocos has function returns file size).
then can use
request->setrequestdata(str.data(), str.size());
php c++ file-upload upload cocos2d-x
No comments:
Post a Comment