rfc1867描述了如何使用http協議來上傳客戶端文件,目前基本上所有的浏覽器和web服務器都支持http文件上傳,它的使用也十分的簡單,具體的來說就是在頁面上創建一個form表單,表單的enctype屬性為multipart/form-data,action為接收上傳文件的cgi url,請求方式為post,在表單中添加type屬性為file的input,file input裡面選擇需要上傳的文件,選擇好後點擊submit,服務器端收到multipart post請求後,會根據相關協議解析請求,然後保存上傳的文件內容,Multipart表單示例:
<form enctype="multipart/form-data" action="http://host:port/UploadFile" method=post>
Upload :<br>
<input name="userfile" type="file"><br>
text field :<input type="text" name="text" value="text"><br>
<input type="submit" value="提交"><input type=reset>
</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
curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
2、添加name/content/contenttype section
curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_CONTENTTYPE, "type", CURLFORM_END);
3、添加 file/filename section
curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_END);
4、添加file/contenttype section
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官網上提供的文件上傳例子:
/* This is an example application source code using the multi interface
* to do a multipart formpost without "blocking". */
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLM *multi_handle;
int still_running;
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";
/* Fill in the file upload field. This makes libcurl load data from
the given file name when curl_easy_perform() is called. */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "sendfile",
CURLFORM_FILE, "postit2.c",
CURLFORM_END);
/* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, "postit2.c",
CURLFORM_END);
/* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
curl = curl_easy_init();
multi_handle = curl_multi_init();
/* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl && multi_handle) {
/* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_multi_add_handle(multi_handle, curl);
curl_multi_perform(multi_handle, &still_running);
do {
struct timeval timeout;
int rc; /* select() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
long curl_timeo = -1;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= 0) {
timeout.tv_sec = curl_timeo / 1000;
if(timeout.tv_sec > 1)
timeout.tv_sec = 1;
else
timeout.tv_usec = (curl_timeo % 1000) * 1000;
}
/* get file descriptors from the transfers */
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
/* In a real-world program you OF COURSE check the return code of the
function calls. On success, the value of maxfd is guaranteed to be
greater or equal than -1. We call select(maxfd + 1, ...), specially in
case of (maxfd == -1), we call select(0, ...), which is basically equal
to sleep. */
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
switch(rc) {
case -1:
/* select error */
break;
case 0:
default:
/* timeout or readable/writable sockets */
printf("perform!\n");
curl_multi_perform(multi_handle, &still_running);
printf("running: %d!\n", still_running);
break;
}
} while(still_running);
curl_multi_cleanup(multi_handle);
/* always cleanup */
curl_easy_cleanup(curl);
/* then cleanup the formpost chain */
curl_formfree(formpost);
/* free slist */
curl_slist_free_all (headerlist);
}
return 0;
}