C++ libcurl upload file / 使用libcurl 做档案上传

Phanix
·
·
IPFS
·

一会儿留着以后还是会处理的

#include <stdio.h>
#include <iostream>
#include <字符串>
#define CURL_STATICLIB
#include "卷曲/卷曲.h"

size_t write_console(void *buffer, size_t size, size_t count, void *userp) {
  std::string str((const char*)buffer, size*count);
  标准::cout<<str;
  返回计数*大小;
}

无效 post_file()
{
  const char* POSTURL = "http://SITE/PATH/TO/CONTROLLER/ACTION";
  卷曲 *curl = curl_easy_init();
  char* pUrlName = "图片.jpg";

  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_console);
  curl_httppost *formpost = 0, *lastptr = 0;

  // 添加参数和文件
  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "File1", CURLFORM_FILE, "Picture.jpg", CURLFORM_FILENAME, pUrlName, CURLFORM_END);
  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "文件名", CURLFORM_COPYCONTENTS, "Picture.jpg", CURLFORM_END);
  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "uid", CURLFORM_COPYCONTENTS, "0012", CURLFORM_END);
  curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "时间戳", CURLFORM_COPYCONTENTS, "1636807618", CURLFORM_END);

  curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

  CURLcode res = curl_easy_perform(curl);
  curl_easy_cleanup(卷曲);
}

诠释主要(无效)
{ /* 在windows中,这将初始化winsock的东西*/
  curl_global_init(CURL_GLOBAL_ALL);
  post_file();
  curl_global_cleanup();
  返回0;
}

原文链接: Phanix 的博客

CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!