标签:libcurl sFilePaths part mime easy post fileInfo 上传 curl
具体用例可以从postman进行复制,比较方便,后台接口api写好以后,在postman运行,然后点击右侧编辑按钮,下拉框选择C-libcurl,下面会自动显示出示例代码:
struct fileInfo
{
fileInfo()
{
}
fileInfo::fileInfo(const fileInfo& other)
{
strFilePath = other.strFilePath;
strFileDir = other.strFileDir;
}
fileInfo& operator = (fileInfo& thInfo)
{
strFilePath = thInfo.strFilePath;
strFileDir = thInfo.strFileDir;
return* this;
}
~fileInfo()
{
}
wstring strFilePath;
wstring strFileDir;
};
bool CurlPostFile(LPCTSTR sUrl, LPCTSTR sToken,LPCTSTR sProjectId,bool bZip,std::vector<fileInfo> vecFileInfo,int& nStatus)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl == nullptr)
{
return false;
}
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, sUrl);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, sToken);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
for (auto iterFile = vecFileInfo.begin(); iterFile != vecFileInfo.end(); ++iterFile)
{
wstring wsAbsFileName = iterFile->strFilePath;
part = curl_mime_addpart(mime);
curl_mime_name(part, "files");
string sFilePath = UnicodeToUtf8(wsAbsFileName);
curl_mime_filedata(part, sFilePath.c_str());
}
string sFilePaths;
for (int i = 0; i < vecFileInfo.size(); ++i)
{
wstring wsDir = vecFileInfo[i].strFileDir;
string sDir = UnicodeToASNI(wsDir.c_str());
sDir = "\"" + sDir + "\"";
if (i != vecFileInfo.size() - 1)
{
sFilePaths = sFilePaths + sDir + ",";
}
else
{
sFilePaths = sFilePaths + sDir;
}
}
sFilePaths = "[" + sFilePaths + "]";
ReplaceString(sFilePaths, "\\", "/");
wstring wsFilePaths = ANSIToUnicode(sFilePaths);
string sUTF8Paths = UnicodeToUtf8(wsFilePaths);
std::wstring wsFilesDir;
part = curl_mime_addpart(mime);
curl_mime_name(part, "filePaths");
curl_mime_data(part, sUTF8Paths.c_str(), CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "projectId");
curl_mime_data(part, sProjectId, CURL_ZERO_TERMINATED);
part = curl_mime_addpart(mime);
curl_mime_name(part, "needUnZip");
if (bZip)
{
curl_mime_data(part, "true", CURL_ZERO_TERMINATED);
}
else
{
curl_mime_data(part, "false", CURL_ZERO_TERMINATED);
}
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
curl_easy_cleanup(curl);
nStatus = (int)res;
if (res != CURLE_OK)
{
return false;
}
return true;
}
标签:libcurl,sFilePaths,part,mime,easy,post,fileInfo,上传,curl 来源: https://blog.csdn.net/m0_37251750/article/details/123082482
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。