c++使用libcurl上传文件

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++使用libcurl上传文件相关的知识,希望对你有一定的参考价值。

1、直接代码

libculr比较成熟,较少出问题,不多说直接上一个简单的示例

#include "stdafx.h"
#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])

	CURL *curl;
	CURLcode res;

	struct curl_httppost *formpost = NULL;
	struct curl_httppost *lastptr = NULL;
	struct curl_slist *headerlist = NULL;
	static const char buf[] = "Expect:";

	curl_global_init(CURL_GLOBAL_ALL);

	/* Fill in the file upload field */
	curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "fileUpload",
		CURLFORM_FILE, "H:\\\\1.xlsx",
		CURLFORM_END);

	/* Fill in the filename field */
	curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "filename",
		CURLFORM_COPYCONTENTS, "1.xlsx",
		CURLFORM_END);

	/* Fill in the submit field too, even if this is rarely needed */
	curl_formadd(&formpost,
		&lastptr,
		CURLFORM_COPYNAME, "submit",
		CURLFORM_COPYCONTENTS, "Submit",
		CURLFORM_END);

	curl = curl_easy_init();
	/* initalize custom header list (stating that Expect: 100-continue is not
	wanted */
	headerlist = curl_slist_append(headerlist, buf);
	if (curl) 
		/* what URL that receives this POST */

		curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:5600/upload");
		if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
			/* only disable 100-continue header if explicitly requested */
			curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
		curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

		/* Perform the request, res will get the return code */
		res = curl_easy_perform(curl);
		/* Check for errors */
		if (res != CURLE_OK)
			fprintf(stderr, "curl_easy_perform() failed: %s\\n",
				curl_easy_strerror(res));

		/* always cleanup */
		curl_easy_cleanup(curl);

		/* then cleanup the formpost chain */
		curl_formfree(formpost);
		/* free slist */
		curl_slist_free_all(headerlist);
	
	return 0;

2、使用nodejs 作为服务端测试

使用multer 来接收上传文件

const express=require('express');
const app=express();
const multer=require('multer');
const bodyParser=require('body-parser');
const fs=require('fs');
const path=require('path');
app.listen(5600,(err)=>
    if(err)
        console.log(err);
    else
        console.log('成功监听5600端口');
);
//获取数据请求  post方式
app.use(bodyParser.urlencoded(
    extended:true,
    maxAge:5*1024*1024
));
app.use(express.static('./www'));
const storage=multer.diskStorage(  //上传属性定制
    destination:function(req,file,cb)
        cb(null,'./upload');
    ,
    filename:function(req,file,cb)  //保存的文件名
        var arr=file.originalname.split('.');
        var ext='.'+arr[arr.length-1]; //后缀名
        var filename='user'+Math.random().toString().replace('.','')+ext;
        cb(null,filename);
    
);
const upload=multer(storage:storage).single('fileUpload');
app.use('/upload',(req,res)=>
    //上传
    upload(req,res,(err)=>
        if(err)//上传时发生错误
            console.log(err);
            return;
        
        //上传完后,一切顺利
        console.log(111);
        res.send('ok');
    );
);

以上是关于c++使用libcurl上传文件的主要内容,如果未能解决你的问题,请参考以下文章

Linux下使用libcurl实现FTP单个文件上传下载功能

C++进阶详解C++开源网络传输库libcurl的编译过程

转:Curl详解

Libcurl的初步实现tfp上传下载功能

c++多线程中使用libcurl库的问题

curl详解