0
我需要將.png文件從cocos2dx上傳到php服務器。 (.TXT是罰款用於測試目的)如何將cocos2dx中的png文件上傳到php服務器
我編輯的引擎喜歡這裏描述:
http://discuss.cocos2d-x.org/t/upload-file-with-httpclient-solved/18028/4 這裏: https://github.com/FenneX/FenneX/commit/134e9433c1dbc3ca6f772ce4c149bf911275a7e9
那麼現在怎麼辦?我怎樣才能將文件上傳到服務器?
這是我的嘗試,但我是http /網絡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";
}
?>