Linux under C language using Libcurl and implementation file upload __oracle

Source: Internet
Author: User
#include <stdio.h> #include <sys/io.h> #include <stdlib.h> #include <errno.h> #include < string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/socket.h> #include <time.h  

> #include <curl/curl.h> #include <sys/stat.h> #include <dirent.h> #include <sys/inotify.h>

static char g_last_name[20]= "Upload_audio.mp3";
int Sendpostfile ();

unsigned long get_file_size ();      
    unsigned long get_file_size () {unsigned long filesize =-1;  
    struct stat statbuff;  
    if (Stat ("/home/user/upload_audio.mp3", &statbuff) < 0) {return filesize;  
    }else{filesize = statbuff.st_size;  
return filesize;
  int Sendpostfile () {CURL *curl;
  Curlcode Res;
  Char filesize[15]={0};

  sprintf (FileSize, "%lu", get_file_size);
  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, Curlfo Rm_copyname, "UploadFile", Curlform_file, G_name, Curlform_contenttype, "application/o
     
   Ctet-stream ", curlform_end);  
   printf ("Curl_formadd filesize\n"); Curl_formadd (&formpost, &lastptr, Curlform_copyname, "filesize", curl 
	
  
   Form_copycontents, FileSize, curlform_end);  
   printf ("Curl_formadd submit\n"); Curl_formadd (&formpost, &lastptr, Curlform_copyname, "Upfile", Curlfo
    
   Rm_copycontents, "commit", curlform_end);
   Uplog ("Curl curl_easy_init\n");
  Curl = Curl_easy_init (); /* Initalize custom Header list (stating that expect:100-continue are not wanted * * Headerlist = Curl_slist_append (headerlist, buf);
    if (Curl) {Uplog ("Curl true begin post\n");
    /* What URL that receives this POST * * curl_easy_setopt (curl, Curlopt_url, post_file_url);
    Curl_easy_setopt (Curl, Curlopt_httpheader, headerlist);
    Curl_easy_setopt (Curl, Curlopt_httppost, formpost); 
    Curl_easy_setopt (Curl, Curlopt_infilesize_large, 1613);
    /* Perform the request, res'll 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_strer
	  Ror (res));
	  Uplog ("Curl url_easy_perform () failed:%s \ n", Curl_easy_strerror (res));
	return-1; 
    }/* Always cleanup * * Curl_easy_cleanup (curl);
    /* Then cleanup the formpost chain * * Curl_formfree (formpost);
  /* Free Slist */Curl_slist_free_all (headerlist);
return 0; 
	} void Main (int argc, char *argv[]) {if (0==sendpostfile ()) {printf ("Sendpostfile success\n"); }else{PrintF ("Sendpostfile failed\n");
 }
}

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.