I need to download file from server , and use it while app is running. Is that possible? Please show solution
I guess you could download it into where getWriteablePath()
stores files for your platform and then at some point before have added that same location to FileUtils
search paths.
string path = cocos2d::FileUtils::getInstance()->getWritablePath()+“font.TTF”;
I saved this on above path, now
string path = cocos2d::FileUtils::getInstance()->getWritablePath()+“font.TTF”;
cocos2d::FileUtils::getInstance()->addSearchPath(path,true);
But it crashes, when I try to access this label and says unable to find file font.ttf
Please help
I don’t think you need this. Just specify the path.
cocos2d::FileUtils::getInstance()->addSearchPath(cocos2d::FileUtils::getInstance()->getWritablePath(), true);
I’m not sure if you should use true
either without taking a look at the source for this function.
No its not working. get crash log:-
Get data from file(font.TTF) failed!
Can you tell me how should I save file downloaded from server using httpresponse. Here is my way :-
oid SplashScene::downloadFontFromServer(){
HttpRequest* request = new (std::nothrow) HttpRequest();
request->setUrl("server url");
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback( [=] (network::HttpClient* client, network::HttpResponse* response) {
if (!response)
{
log("onHttpRequestCompleted - No Response");
return;
}
log("onHttpRequestCompleted - Response code: %lu", response->getResponseCode());
if (!response->isSucceed())
{
log("onHttpRequestCompleted - Response failed");
log("onHttpRequestCompleted - Error buffer: %s", response->getErrorBuffer());
return;
}
std::vector<char> *buffer = response->getResponseData();
CCLOG("buffer size %ld ", buffer->size());
string ret(&(buffer->front()), buffer->size());
CCLOG("%s", ("Response message: " + ret).c_str());
std::string dataFromServer(buffer->begin(),buffer->end());
//save file here
string path = cocos2d::FileUtils::getInstance()->getWritablePath()+"font.TTF";
cocos2d::FileUtils::getInstance()->addSearchPath(cocos2d::FileUtils::getInstance()->getWritablePath(),true);
// CCLOG("pathfor saving file %s",path.c_str());
FILE *fp = fopen(path.c_str(), "w");
if (! fp)
{
CCLOG("can not create file %s", path.c_str());
return "";
}
fputs(file, fp);
fclose(fp);
can you tell where I am wrong?
I’d place some breakpoints and see what is happening. your path variable still looks wrong and then you try to open it with FILE?