下面是使用libcurl库实现文件上传的一个实例:

void CDataProcess::sendFileToServer(void)
{
string netIp = strNetUrl + "SaveFile"; for (int i = ; i < taskZipVector.size(); ++i)
{
string strFileName = taskZipVector[i];
string strFilePath = strAppPath + "\\Task\\"+ strFileName + ".zip"; CURL *curl = NULL;
string readFile = ""; struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:"; if(- == _access(strFilePath.c_str(),))
{
return ;
}
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "fileContent",
CURLFORM_FILE, strFilePath.c_str(),
CURLFORM_END); curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, strFileName.c_str(),
CURLFORM_END); curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END); curl = curl_easy_init();
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_URL, netIp.c_str()); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
//CFuncs::WriteLogInfo(SLT_ERROR, "finshUpdate() curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return ;
} isSendedFlag = ; //设置已传送文件标志
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all (headerlist);
}
}

struct curl_httppost {
  struct curl_httppost *next; /* next entry in the list */
  char *name; /* pointer to allocated name */
  long namelength; /* length of name length */
  char *contents; /* pointer to allocated data contents */
  long contentslength; /* length of contents field, see also
  CURL_HTTPPOST_LARGE */
  char *buffer; /* pointer to allocated buffer contents */
  long bufferlength; /* length of buffer field */
  char *contenttype; /* Content-Type */
  struct curl_slist *contentheader; /* list of extra headers for this form */
  struct curl_httppost *more; /* if one field name has more than one
  file, this link should link to following
  files */
  long flags; /* as defined below */

  /* specified content is a file name */
  #define CURL_HTTPPOST_FILENAME (1<<0)
  /* specified content is a file name */
  #define CURL_HTTPPOST_READFILE (1<<1)
  /* name is only stored pointer do not free in formfree */
  #define CURL_HTTPPOST_PTRNAME (1<<2)
  /* contents is only stored pointer do not free in formfree */
  #define CURL_HTTPPOST_PTRCONTENTS (1<<3)
  /* upload file from buffer */
  #define CURL_HTTPPOST_BUFFER (1<<4)
  /* upload file from pointer contents */
  #define CURL_HTTPPOST_PTRBUFFER (1<<5)
  /* upload file contents by using the regular read callback to get the data and
  pass the given pointer as custom pointer */
  #define CURL_HTTPPOST_CALLBACK (1<<6)
  /* use size in 'contentlen', added in 7.46.0 */
  #define CURL_HTTPPOST_LARGE (1<<7)

  char *showfilename; /* The file name to show. If not set, the
  actual file name will be used (if this
  is a file part) */
  void *userp; /* custom pointer used for
  HTTPPOST_CALLBACK posts */
  curl_off_t contentlen; /* alternative length of contents
  field. Used if CURL_HTTPPOST_LARGE is
  set. Added in 7.46.0 */
};

struct curl_slist {
  char *data;
  struct curl_slist *next;
};

CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,.);

libcurl库的简介(二)的更多相关文章

  1. libcurl库的简介(一)

    一.Libcurl库简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP ...

  2. Python数据分析Numpy库方法简介(二)

    数据分析图片保存:vg 1.保存图片:plt.savefig(path) 2.图片格式:jpg,png,svg(建议使用,不失真) 3.数据存储格式: excle,csv csv介绍 csv就是用逗号 ...

  3. Linux之Libcurl库的介绍与应用20170509

    一.LibCurl简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等 ...

  4. C++ 用libcurl库进行http通讯网络编程

    使用libcurl完成http通讯,很方便而且是线程安全,转载一篇比较好的入门文章 转载自 http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724 ...

  5. C++ 用libcurl库进行http通讯网络编程(转)

    转载:http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三. ...

  6. C++ 用libcurl库进行http通讯网络编程[转]

    http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.cur ...

  7. C/C++ 用libcurl库进行http通讯网络编程

    C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...

  8. C++ 用libcurl库进行http 网络通讯编程

      一.LibCurl基本编程框架libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcur ...

  9. Raphael Js矢量库API简介:

    Raphael Js矢量库API简介:Raphael Javascript 是一个 Javascript的矢量库. 2010年6月15日,著名的JavaScript库ExtJS与触摸屏代码库项目jQT ...

随机推荐

  1. Harris角点检测理论

     这样想象一下,直线上的一个点在垂直于直线的方向上有最强的梯度.沿着直线的方向梯度较低,意思是直线上的像素点与它周围的像素点看起来相似.我们进行的角点检测是梯度强度明显高于其他像素的点,可能就是目标处 ...

  2. Date/Math/String对象的函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 在cc.EventListener.TOUCH_ONE_BY_ONE事件中判断拖动物离哪边近飞向那边

    需将拖动物坐标和要飞向物坐标转化为整个layer的坐标,因为人们判断远近是根据整个layer来看的.

  4. cpu几核

    import psutil def GetCpuInfo(): cpu_count = psutil.cpu_count(logical=False) #1代表单核CPU,2代表双核CPU xc_co ...

  5. CodeForces -1216B.Shooting

    水题 #include <cstdio> #include <algorithm> using namespace std; ; struct node{ int s, f; ...

  6. css+div上下左右自适应居中

    主要记录自己日常积累的布局相关的东西,持续更新中. 1.登录框上下左右自适应居中 以前想要把登录表单始终放置在页面的中间,花了不少心思,一直以来用的解决方法都是用js,感觉有点麻烦不是很好,于是在网上 ...

  7. LoadRunner使用记录

    基本术语 性能测试--通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项性能指标进行测试. 负载测试和压力测试都属于性能测试,两者可以结合进行. 负载测试,确定在各种工作负载下系统的 ...

  8. 解决 genymotion 安装apk报错 app contains ARM native code and your Genymotion device cannot run ARM instructions

    1.某些APP安装在模拟器时提示“ this probably means that the app contains ARM native code and your Genymotion devi ...

  9. VSFTPD服务器

    我虽然vsftpd服务器做了百遍以上,但是我觉的成功率不大.我要写下这篇日记,来让我分析自己曾经的过错!! vsftpd的原理我不多讲!!!请自行百度吧!! 我搭建的环境是 Linux6.4 IP(1 ...

  10. 在Unity3d中使用Google.ProtocolBuffers

    通过Nuget下载Google.ProtocolBuffers,在目录中找到net35下的文件,放入unity3d中作为插件 PersonMessage.Builder personBuilder = ...