curl的http上传文件代码

int http_post_file(const char *url, const char *user, const char *pwd, const char *filename)
{
    assert(url != NULL);
    assert(user != NULL);
    assert(pwd != NULL);
    assert(filename != NULL);

int ret = -1;
    CURL *curl = NULL;
    CURLcode code;
    CURLFORMcode formCode;
    int timeout = 15;

#define CHECK_FORM_ERROR(x)                                                /
    if ((formCode = (x)) != CURL_FORMADD_OK)                            /
    {                                                                    /
        fprintf(stderr, "curl_formadd[%d] error./n", formCode);            /
        goto out;                                                        /
    }

#define CHECK_SETOPT_ERROR(x)                                            /
    if ((code = (x)) != CURLE_OK)                                        /
    {                                                                    /
        fprintf(stderr, "curl_easy_setopt[%d] error./n", code);            /
        goto all;                                                        /
    }

struct curl_httppost *post=NULL;
    struct curl_httppost *last=NULL;
    struct curl_slist *headerlist=NULL;

CHECK_FORM_ERROR( curl_formadd(&post, &last, CURLFORM_COPYNAME, "user",
        CURLFORM_COPYCONTENTS, user,
        CURLFORM_END));

CHECK_FORM_ERROR( curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
        CURLFORM_COPYCONTENTS, pwd,
        CURLFORM_END));

CHECK_FORM_ERROR( curl_formadd(&post, &last, CURLFORM_COPYNAME, "file",
        CURLFORM_FILE, filename,
        CURLFORM_END));

CHECK_FORM_ERROR( curl_formadd(&post, &last,
        CURLFORM_COPYNAME, "submit",
        CURLFORM_COPYCONTENTS, "upload",
        CURLFORM_END));

curl = curl_easy_init();
    if(curl == NULL)
    {
        fprintf(stderr, "curl_easy_init() error./n");
        goto out;
    }

CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HEADER, 0));
    CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_URL, url));
    CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_HTTPPOST, post));
    CHECK_SETOPT_ERROR(curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout));

code = curl_easy_perform(curl);
    if(code != CURLE_OK)
    {
        fprintf(stderr, "curl_easy_perform[%d] error./n", code);
        goto all;
    }

ret = 0;

all:
    curl_easy_cleanup(curl);
out:
    curl_formfree(post);

return ret;
}

时间: 2024-08-29 22:26:46

curl的http上传文件代码的相关文章

jquery ajax实现上传文件代码,带进度条

原文:jquery ajax实现上传文件代码,带进度条 源代码下载地址:http://www.zuidaima.com/share/1550463291116544.htm ajax上传文件代码,带进度条的. 首页 http://localhost:端口/项目名/common/test.htm 上传中 标签: jquery ajax 上传 进度条话题: Web开发 前端技术 jquery ajax实现上传文件代码,带进度条

Drupal创建自定义表单,上传文件代码

Drupal中创建自定义表单,用来上传文件,对上传文件做一些操作.以下是放在Module中的代码: 一.菜单建立表单路径 /** Implementation of hook_menu(). */ function moduleName_menu () { $items = array(); $items['admin/import'] = array( 'title' => 'title', 'page callback' => 'drupal_get_form', 'page argume

ExtJS + fileuploadfield上传文件代码

后台服务端接收文件的代码: /** * 后台上传文件处理Action */ @RequestMapping(value = "/uploadFile", method=RequestMethod.POST) public void uploadFile(@RequestParam(value="file",required=true) MultipartFile file ,HttpServletResponse response) { ModelMap model

PHP curl 模拟POST 上传文件

<?php /** * Email [email protected] * author jackluo * 2014.11.21 * */ //* function curl_post($url, $data, $header = array()){ if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if(is_array($header) && !e

PHP上传文件代码练习2 (重复文章)

表单: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题</title> </head> <body> <form action="upload.php" method="post" enctype=

上传文件代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

javaWeb上传文件代码

javaweb两种方式的上传,1普通上传,2:jquery ajax后台上传,部分截图如下: 完成包下载,下载后倒入myeclipse工程即可,下载地址:http://files.cnblogs.com/files/haha12/uploadDemo.rar

easyui 上传文件代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using BLL;using m = Model;using System.Data;using System.Data.SqlClient;using System.Text;namespace Web.Handler{ /// <summary> /// AddOppportunityHandle

socket 上传文件代码

server.py #!/usr/bin/env python# -*- coding:utf-8 -*- import socketimport os,hashlib ip_port = ('127.0.0.1',6969)sk = socket.socket()sk.bind(ip_port)sk.listen(5) while True: conn,address = sk.accept() while True: print('等待新指令') #获取客户端发来的操作指令 data = c