cocos怎么把res文件夹放服务器上,如何将cocos2dx中的png文件上传到php服务器

堆栈感谢这个 '伟大的答案' 溢出...

客户端代码:

//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<: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 *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代码:(当然这是用于测试目的)

$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";

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值