cocos2d-x 文件上传

本身框架内没有实现文件上传功能,现在ios和mac的可以直接调用oc实现,但是android端就很麻烦,因为需要jni但是,jni又不能直接回调我传入的response callback,就必须要修改android版本的http client代码,此外还尝试了自己模拟 multi-part 的表单数据,但是结果一直不对,请问大家有谁实现过类似的功能吗?

可以考虑改用curl实现,自己新写或者参考quick-cocos2d-x中CCHTTPRequest基于curl实现的部分,多少都得自己去改写一些代码实现

是 就是也看到这块了 现在cocos2dx 是把实现分离开了 http-client-android , http-client-ios 这样子的,实际上就是要分两块进行实现,这么多年了一个上传文件的功能,开发组都不给加上去,还要我们自己手动做

搞了几天自己拼接这个multipart-formdata的body 还是解决了的,我也把代码上传上来以后有哪些朋友想用的就直接用了。

auto req = createRequest(“127.0.0.1:8080/v1/upload”, {}, [=](bool ok, const std::string& resp) {
});
req->setRequestType(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<std::string> contentType;
contentType.push_back("Content-Type: multipart/form-data; boundary=" + boundary);

//Set the header with the previous content type

//Build the body of the request. Include the boundary, basename and file name.
//Specify the content disposition and type
req->setHeaders(contentType);
auto data = FileUtils::getInstance()->getDataFromFile("fps_images.png");
std::string s;
s = s + "\r\n--" + boundary + "\r\n";
s = s + "Content-Disposition: form-data; name=\"" + "file"
    + "\"; filename=\"" + "jj.txt" + "\"\r\n";
s = s + "Content-Type: application/octet-stream\r\n\r\n";

//Then append the file data and again the boundary
std::string s1;
s1 = s1 +  + "\r\n";
s1 = s1 + "--" + boundary + "\r\n";
s1 = s1 + "Content-Disposition: form-data; name=\"" + "token"
    + "\"\r\n\r\n" + "23794hihi";
s1 = s1 + "\r\n--" + boundary + "--\r\n";


int len = s1.size() + s.size() + data.getSize();
char* buff = new char[len];
memcpy(buff, s.data(), s.size());
memcpy(buff + s.size(), data.getBytes(), data.getSize());
memcpy(buff+ s.size() + data.getSize(), s1.data(), s1.size());
req->setRequestData(buff,len);

auto headers = req->getHeaders();
headers.push_back(“Content-Type: multipart/form-data; boundary=” + b);
headers.push_back(“Content-Length:” + std::to_string(len));
HttpClient::getInstance()->send(req);

2赞

重新整理了一下代码看起来更清楚些,如果需要多个文件或者更多参数,可以参考我的代码自行进行扩展了.

std::string boundary = "--------------------------569509022818701499338160";

auto req = createRequest("127.0.0.1:8080/v1/upload", {}, [=](bool ok, const std::string& resp) {
    });
req->setRequestType(HttpRequest::Type::POST);

auto data = FileUtils::getInstance()->getDataFromFile("fps_images.png");
std::string key = "file";
std::string fileName = "fps_images.png";
std::string s;
s = s + "\r\n--" + boundary + "\r\n";
s = s + "Content-Disposition: form-data; name=\"" + key
    + "\"; filename=\"" + fileName + "\"\r\n";
s = s + "Content-Type: application/octet-stream\r\n\r\n";

//Then append the file data and again the boundary
std::string pKey = "token";
std::string pValue = "23794hihi";
std::string parameters;
parameters = parameters +  + "\r\n";
parameters = parameters + "--" + boundary + "\r\n";
parameters = parameters + "Content-Disposition: form-data; name=\"" + pKey
    + "\"\r\n\r\n" + pValue;
parameters = parameters + "\r\n--" + boundary + "--\r\n";


int len = parameters.size() + s.size() + data.getSize();
char* buff = new char[len];
memcpy(buff, s.data(), s.size());
memcpy(buff + s.size(), data.getBytes(), data.getSize());
memcpy(buff+ s.size() + data.getSize(), parameters.data(), s1.size());
req->setRequestData(buff,len);

auto headers = req->getHeaders();
headers.push_back("Content-Type: multipart/form-data; boundary=" + boundary);
headers.push_back("Content-Length:" + std::to_string(len));
HttpClient::getInstance()->send(req);

该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。