您当前的位置: 首页 >  c++

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c++使用libcurl上传文件

qianbo_insist 发布时间:2022-06-11 16:06:14 ,浏览量:0

1、直接代码

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

#include "stdafx.h"
#include 
#include 
#include 
#include 
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');
    });
});
关注
打赏
1663161521
查看更多评论
立即登录/注册

微信扫码登录

0.0397s