使用libcurl進行文件上傳


上篇博文講到了如何使用multicurl來進行http並發訪問,今天繼續有關curl的主題,來八一八如何使用curl來上傳文件,在介紹具體方法之前了解下目前http文件上傳的基本實現。

    rfc1867描述了如何使用http協議來上傳客戶端文件,目前基本上所有的瀏覽器和web服務器都支持http文件上傳,它的使用也十分的簡單,具體的來說就是在頁面上創建一個form表單,表單的enctype屬性為multipart/form-data,action為接收上傳文件的cgi url,請求方式為post,在表單中添加type屬性為file的input,file input里面選擇需要上傳的文件,選擇好后點擊submit,服務器端收到multipart post請求后,會根據相關協議解析請求,然后保存上傳的文件內容,Multipart表單示例:

[html]  view plain copy
 
  1. <form enctype="multipart/form-data" action="http://host:port/UploadFile" method=post>  
  2.     Upload :<br>  
  3.     <input name="userfile" type="file"><br>  
  4.     text field :<input type="text" name="text" value="text"><br>  
  5.     <input type="submit" value="提交"><input type=reset>  
  6. </form>  


    好了,現在來講一講curl的文件上傳,對於curl來講,其實它要完成的任務就是構建一個multipart/formdata HTTP POST請求。類似於往multipart form表單中添加type為file或者text的input item一樣,curl也需要我們構造表單中的input item,curl_formadd函數可以幫助我們完成這個任務,它即可以添加普通的name-value section,也可以添加file upload section,下面舉幾個具體例子:

1、添加name/content section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",   CURLFORM_COPYCONTENTS, "content", CURLFORM_END);  

2、添加name/content/contenttype section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",   CURLFORM_COPYCONTENTS, "content",   CURLFORM_CONTENTTYPE, "type", CURLFORM_END);  

3、添加 file/filename section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic",   CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_END);     

4、添加file/contenttype section

[cpp]  view plain copy
 
  1. curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic",   CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic",  CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);  


     上面的post 和 last都是指向curl_httppost對象的指針, post指向的就是一個由所有section組成的鏈表的開端,last是該鏈表的尾指針。當我們添加完所有的form section之后,使用curl_easy_setopt(curl, CURLOPT_HTTPPOST,post)函數設置curl的http post,最后就是調用curl_easy_perform執行請求。需要注意的是,當使用libcurl的POST方式時,如果POST數據的大小大於1024個字節,libcurl不會直接發送POST請求,而是會分為兩步執行請求:
    1、發送一個請求,該請求頭部包含一個Expect: 100-continue的字段,用來詢問server是否願意接受數據
    2、當接收到從server返回的100-continue的應答后,它才會真正的發起POST請求,將數據發送給server。
    對於文件上傳來說,文件大小往往會超過1024個字節,所以如果你確認你的服務器不會拒絕你的文件上傳請求的話,可以禁止curl的Expect請求頭,具體方法可以去看看我的另外一篇文章《libcurl的使用問題“Expect100-continue” 》。

    最后附上curl官網上提供的文件上傳例子:

[cpp]  view plain copy
 
    1. /* This is an example application source code using the multi interface 
    2.  * to do a multipart formpost without "blocking". */  
    3. #include <stdio.h>  
    4. #include <string.h>  
    5. #include <sys/time.h>  
    6.   
    7. #include <curl/curl.h>  
    8.   
    9. int main(void)  
    10. {  
    11.   CURL *curl;  
    12.   
    13.   CURLM *multi_handle;  
    14.   int still_running;  
    15.   
    16.   struct curl_httppost *formpost=NULL;  
    17.   struct curl_httppost *lastptr=NULL;  
    18.   struct curl_slist *headerlist=NULL;  
    19.   static const char buf[] = "Expect:";  
    20.   
    21.   /* Fill in the file upload field. This makes libcurl load data from 
    22.      the given file name when curl_easy_perform() is called. */  
    23.   curl_formadd(&formpost,  
    24.                &lastptr,  
    25.                CURLFORM_COPYNAME, "sendfile",  
    26.                CURLFORM_FILE, "postit2.c",  
    27.                CURLFORM_END);  
    28.   
    29.   /* Fill in the filename field */  
    30.   curl_formadd(&formpost,  
    31.                &lastptr,  
    32.                CURLFORM_COPYNAME, "filename",  
    33.                CURLFORM_COPYCONTENTS, "postit2.c",  
    34.                CURLFORM_END);  
    35.   
    36.   /* Fill in the submit field too, even if this is rarely needed */  
    37.   curl_formadd(&formpost,  
    38.                &lastptr,  
    39.                CURLFORM_COPYNAME, "submit",  
    40.                CURLFORM_COPYCONTENTS, "send",  
    41.                CURLFORM_END);  
    42.   
    43.   curl = curl_easy_init();  
    44.   multi_handle = curl_multi_init();  
    45.   
    46.   /* initalize custom header list (stating that Expect: 100-continue is not 
    47.      wanted */  
    48.   headerlist = curl_slist_append(headerlist, buf);  
    49.   if(curl && multi_handle) {  
    50.   
    51.     /* what URL that receives this POST */  
    52.     curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");  
    53.     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);  
    54.   
    55.     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
    56.     curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
    57.   
    58.     curl_multi_add_handle(multi_handle, curl);  
    59.   
    60.     curl_multi_perform(multi_handle, &still_running);  
    61.   
    62.     do {  
    63.       struct timeval timeout;  
    64.       int rc; /* select() return code */  
    65.   
    66.       fd_set fdread;  
    67.       fd_set fdwrite;  
    68.       fd_set fdexcep;  
    69.       int maxfd = -1;  
    70.   
    71.       long curl_timeo = -1;  
    72.   
    73.       FD_ZERO(&fdread);  
    74.       FD_ZERO(&fdwrite);  
    75.       FD_ZERO(&fdexcep);  
    76.   
    77.       /* set a suitable timeout to play around with */  
    78.       timeout.tv_sec = 1;  
    79.       timeout.tv_usec = 0;  
    80.   
    81.       curl_multi_timeout(multi_handle, &curl_timeo);  
    82.       if(curl_timeo >= 0) {  
    83.         timeout.tv_sec = curl_timeo / 1000;  
    84.         if(timeout.tv_sec > 1)  
    85.           timeout.tv_sec = 1;  
    86.         else  
    87.           timeout.tv_usec = (curl_timeo % 1000) * 1000;  
    88.       }  
    89.   
    90.       /* get file descriptors from the transfers */  
    91.       curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);  
    92.   
    93.       /* In a real-world program you OF COURSE check the return code of the 
    94.          function calls.  On success, the value of maxfd is guaranteed to be 
    95.          greater or equal than -1.  We call select(maxfd + 1, ...), specially in 
    96.          case of (maxfd == -1), we call select(0, ...), which is basically equal 
    97.          to sleep. */  
    98.   
    99.       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);  
    100.   
    101.       switch(rc) {  
    102.       case -1:  
    103.         /* select error */  
    104.         break;  
    105.       case 0:  
    106.       default:  
    107.         /* timeout or readable/writable sockets */  
    108.         printf("perform!\n");  
    109.         curl_multi_perform(multi_handle, &still_running);  
    110.         printf("running: %d!\n", still_running);  
    111.         break;  
    112.       }  
    113.     } while(still_running);  
    114.   
    115.     curl_multi_cleanup(multi_handle);  
    116.   
    117.     /* always cleanup */  
    118.     curl_easy_cleanup(curl);  
    119.   
    120.     /* then cleanup the formpost chain */  
    121.     curl_formfree(formpost);  
    122.   
    123.     /* free slist */  
    124.     curl_slist_free_all (headerlist);  
    125.   }  
    126.   return 0;  
    127. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM