cocos Httpclient 上传文件到服务器

12 篇文章 0 订阅
废话不多说、直接贴代码!
#include "Test01060Http.h"
#include <Shlwapi.h>
#include "stdio.h"




Test01060Http::Test01060Http()
{
}




Test01060Http::~Test01060Http()
{
}


bool Test01060Http::init()
{
Layer::init();


UploadPictures();


return true;
}
#define MYDATA "this is extra data from client"
void  Test01060Http::UploadPictures()
{
        network::HttpClient* http = network::HttpClient::getInstance();
network::HttpRequest* req = new network::HttpRequest;


req->setRequestType(network::HttpRequest::Type::POST);
req->setUrl("http:///subassembly/uploadPicterController/upload.html");
req->setResponseCallback(CC_CALLBACK_2(Test01060Http::onHttpRequestCompleted, this));


std::string pathKey = FileUtils::sharedFileUtils()->fullPathForFilename("b2de9c82d158ccbf0881c1d01dd8bc3eb135411e.jpg");
CCLOG("path = %s",pathKey.c_str());


//auto sifile = PathFindFileName(pathKey);
    Data imgdata = FileUtils::getInstance()->getDataFromFile(pathKey);

LPTSTR a = (LPTSTR)pathKey.c_str();

        /  / 拿到最后一个斜杠最后文件名实例!

std::string B = "C:\\Windows\\System32/notepad.exe";
ssize_t pos = B.find_last_of("/");
std::string cach_filename;
if (pos !=std::string::npos)
{
cach_filename = B.substr(pos + 1);
}
else
{
cach_filename = B;
}

CCLOG("B = %s", cach_filename.c_str());


        //拿到图片数据

        long buff = 0;
unsigned char * pBuffer = FileUtils::sharedFileUtils()->getFileData(pathKey.c_str(),"rb", &buff);
const char* fileBinary = (const char*)pBuffer;
CCLOG("fileBinary %s = ", fileBinary);
       //得到图片 文件流大小
std::string strBin = std::string(fileBinary, buff);
       //设置 http headers  参数 和  数据流
std::string boundary = "----------------WebKitFormBou3123ndaryEm5WNw6hGiQUBpng";
std::string bound = boundary;
std::vector<std::string> headers;
headers.push_back("Content-Type:multipart/form-data; boundary = " + bound);
std::string strdata = strBin;
std::string str = "\r\n--" + boundary + "\r\n";
str = str + "Content-Disposition: form-data; name=\"file\"; filename=\"paoku1.png\"\r\n";
str = str + "Content-Type: application/octet-stream\r\n\r\n";
str = str + strBin;
str = str + "\r\n--" + boundary + "--\r\n";

// 设置设置Header

req->setHeaders(headers);

     //设置 RequesData 
req->setRequestData(str.data(), str.size());

CCLOG("str data = %s \n str .size = %d \n", str.data(), str.size());

//发送 

http->send(req);

//关闭

req->release();
};
void  Test01060Http::onHttpRequestCompleted(network::HttpClient* client, network::HttpResponse* response)
{
if (!response->isSucceed())
{
CCLOG("error");
CCLOG("error buffer: %s", response->getErrorBuffer());
CCLOG("error code: %d", (int)response->getResponseCode());
return;
}
CCLOG("login success");
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值