c++ - 如何使用 libcurl 将图片上传到最重要的服务器?
问题描述
尝试构建一个 C++ 应用程序以将图像上传到最重要的服务器(版本 5.14.0)。我已经使用我的 C++ 代码获得了访问令牌、通道 ID。但我无法使用 libcurl 上传图像。这是我的 CPP 代码(现在使用 libcurl):
void uploadPicture(string url,string channelId, string path,string fileName,string token)
{
url += "/api/v4/files";
CURL *curl;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "files",
CURLFORM_FILE, path.c_str(),
CURLFORM_FILENAME,"test.jpg",
CURLFORM_CONTENTTYPE, "image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "client_ids",
CURLFORM_COPYCONTENTS, "test.jpg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "channel_id",
CURLFORM_COPYCONTENTS, channelId.c_str(),
CURLFORM_END);
struct curl_slist *headers = NULL;
string auth = "Authorization:Bearer ";
auth += token;
headers = curl_slist_append(headers, "Content-Type:multipart/form-data");
headers = curl_slist_append(headers, auth.c_str());
CURLcode res;
curl = curl_easy_init();
if (curl)
{
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_setopt(curl, CURLOPT_HTTPPOST,formpost);
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getFileInfo);
res = curl_easy_setopt(curl, CURLOPT_POST, 1);
res = curl_easy_perform(curl);
if (res != 0)
{
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
}
}
但它不会工作......请告诉我问题出在哪里。
解决方案
最后。我找到了解决方案:
void uploadByMimeApi(string url, string channelId, string path, string fileName, string token)
{
url += "/api/v4/files";
CURL *curl = curl_easy_init();
curl_mime *mime;
curl_mimepart *part;
curl_mimepart *part2;
curl_mimepart *part3;
/* Build an HTTP form */
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "files");
curl_mime_filedata(part, path.c_str());
curl_mime_type(part, "image/jpeg");
part2 = curl_mime_addpart(mime);
curl_mime_data(part2, channelId.c_str(), CURL_ZERO_TERMINATED);
curl_mime_name(part2, "channel_id");
part3 = curl_mime_addpart(mime);
curl_mime_data(part3, "test", CURL_ZERO_TERMINATED);
curl_mime_name(part3, "client_ids");
//header issue
struct curl_slist *headers = NULL;
static const char buf[] = "Expect:";
string auth = "Authorization:Bearer ";
auth += token;
headers = curl_slist_append(headers, auth.c_str());
headers = curl_slist_append(headers, buf);
/* Post and send it. */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getFileInfo);
curl_easy_perform(curl);
/* Clean-up. */
curl_easy_cleanup(curl);
curl_mime_free(mime);
}
推荐阅读
- javascript - 如何在mvc中使用ajax插入图像
- python - 如何将熊猫数据框附加到 Excel 工作表
- javascript - vue.js 中的模板绑定到什么?
- sql - 在 CREATE TABLE 语句中使用用户定义的函数
- c++ - 使用 istringstearm 读取文件缓慢并获取行
- javascript - Javascript:当在promise中设置状态时,得到一个无休止的输出循环
- keras - keras Tuner AttributeError:模块'tensorflow._api.v1.keras.metrics'没有属性'Metric'
- xamarin - 上面有数字的按钮 - 我可以在不使用 Grid.Row 的情况下创建它吗
- c# - 来自 uri/ 帖子中的查询字符串的 WEB API 帖子
- nestjs - 如何使用配置对象和依赖项创建 NestJs 管道?