C Language HTTP File Upload

Source: Internet
Author: User
Tags http file upload sendfile

Generally, the C language is rarely used to directly upload files. But what should I do when I use C programming to upload files?

With the open-source libcurl library, we can easily implement this function. Libcurl is a free and easy-to-use client URL transfer library. Its main function is to connect and communicate with different servers using different protocols. libcurl currently supports dict, file, FTP, ftps, Gopher, HTTP, HTTPS, IMAP, IMAPs, LDAP, LDAPS, POP3, pop3s, rtmp, RTSP, SCP, SFTP, SMTP, smtps, Telnet andtftp. Libcurl also supports HTTPS certificate authorization, http post,
Http put, FTP upload, HTTP basic form upload, proxy, cookies, and user authentication. The following uses the libcurl official website as an example to complete simple file upload.

Simulate the File Upload form to be implemented:

<form action="fileUpload.action" method="post" enctype="multipart/form-data">File:<input type="file" name="sendfile" /><br> FileName:<input type="text" name="filename" /><br> <input type="submit" name="submit" value="Submit" /></form>

Among them, fileupload. Action is the interface for processing file uploads, Which is configured according to actual needs. Here is just an example.

The code for uploading files through HTTP in C language is as follows:

#include <stdio.h>#include <string.h>#include <curl/curl.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, "sendfile",               CURLFORM_FILE, "D:\\sign.txt",               CURLFORM_END);  /* Fill in the filename field */  curl_formadd(&formpost,               &lastptr,               CURLFORM_COPYNAME, "filename",               CURLFORM_COPYCONTENTS, "sign.txt",               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:8080/fileUpload.action");    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;}

The Code has been tested and can be used. However, you need to configure the libcur Library and the compiling environment in advance. The code is very rough and the function is very simple. It just plays a role and is expected to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.