下面是使用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. Virtual DOM(八)

    Virtual DOM 这个概念相信大部分人都不会陌生,它产生的前提是浏览器中的 DOM 是很“昂贵"的,为了更直观的感受,我们可以简单的把一个简单的 div 元素的属性都打印出来,如图所示 ...

  2. pick the stone game

    我该如何去触摸这类问题嘞! 取石子游戏 1堆石子有n个,两人轮流取. 先取者第1次可以取任意多个,但不能全部取完. 以后每次取的石子数不能超过上次取子数的2倍. 取完者胜.先取者负输出"Se ...

  3. blob - 二进制文件流下载

    /** * 返回值文件类型为 blob 二进制流文件 * responseType: 'blob' * params 接口所需参数 * 命名文件名:依据时间戳命名文件名 * (导出时需要延迟,否则导出 ...

  4. Python记:列表方法略记

  5. open函数 文件设置缓冲

    # 注释 将文件写入硬件设备时,使用系统调用,这类I/O操作一般时间很长 # 为了减少I/O次数操作,文件通常使用缓冲区(有足够的数据才进行系统调用) # 文件缓冲行为分为: # 全缓冲: open函 ...

  6. telnet不起作用

    1.出现 'telnet' 不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:因为本机的Telnet客户端默认是关闭的,所以我们要手动打开 解决方案:打开控制面板–>程序–>打开 ...

  7. HTML的网页基本结构

    写在前面 <!DOCTYPE html><html lang="en"><head>            <meta charset=& ...

  8. AcWing 900. 整数划分

    #include <iostream> #include <algorithm> using namespace std; , mod = 1e9 + ; int n; int ...

  9. JDK8新特性---stream流

    项目上用到了stream流,找篇blog,转载一下,介绍下Stream流的用法. 1 流概述  流是 JDK8 新增的成员,允许以声明性方式处理数据集合,可以把 Stream 流看作是遍历数据集合的一 ...

  10. Centos7 安装 Cockpit

    1sudo yum -y install epel-release sudo yum -y update sudo shutdown -r now 2yum -y install cockpit sy ...