libCurl的檔案上傳

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   on   檔案   

最近在需要使用curl的上傳功能,使用libCurl來實現。因此,先使用curl命令操作,然後再使用libCurl實現。

基於Http協議的檔案上傳的標準方法是: 基於POST Form的檔案上傳  RFC1867。

這個方法使用非常廣泛,這個RFC規定了FORM上傳檔案的標準方法,如下介紹了基於libcurl來開發upload功能。

開發實現過程 

                     1. 使用curl 命令列執行代碼,  2. 跟蹤分析 curl的request和response, 3.使用libCurl的API進行開發實現

0. 搭建  upload server

   --- 這個請google一些WebServer的搭建,已經參考RFC1867 或網上例子選擇任意一種語言,編寫網路後太處理代碼。

    我使用python的django framework編寫了一個upload處理頁面,

   需要FORM的欄位 

    Title-newFileNameInServer

    [email protected]/abc/

    url http://192.168.0.61/due/upload/

1. 使用Curl 上傳檔案的命令

                  curl -F"title=dddd.txt" -F "[email protected]/home/chenglun/upload/abc" http://192.168.0.61/due/upload/

2. 分析

                curl --trace trace.log -F"title=dddd.txt" -F "[email protected]/home/chenglun/upload/abc" http://192.168.0.61/due/upload/

log:

=> connect....

=> Send header, 298 bytes (0x12a)

POST /due/upload  ......  bound.....

<= Recv header, 23 bytes (0x17)

HTTP/1.1 100 Continue..

=> Send data, 249 bytes (0xf9)

bound...

Content-Disposition:form-data; name ....

....

bound...

<= Recv header, 17 bytes (0x11)

HTTP/1.1 200 OK..

..............

從trace中可以知道,只需要building一個form,加入對應的Key=value部分然後使用curl的request即可實現如上傳輸過程。

3. 使用libCurl實現

使用API有:   curl_formadd(); curl_formfree 產生 一個from,然後使用 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);設定curl,然後執行,即可。

curl_formadd()使用參考, libCurl的文檔,文檔有例子的。

代碼上半部:

int main() {
    CURL *curl;
    CURLcode res;
    struct data config;
    struct stat file_info; 

    const char * localfile = "/home/chenglun/upload/abc"";
    FILE * fp = fopen(localfile, "rb"); 
    if(!fp)    {
        return -1;
    }

    if(fstat(fileno(fp), &file_info) != 0)    {
        return -1;
    }

    curl = curl_easy_init();

    if(curl) {
        struct curl_httppost* post = NULL;
        struct curl_httppost* last = NULL;

        const char * remoteNewFileKey = "title";
        const char * remoteNewFileName = "d.ttt";
        curl_formadd(&post, &last, CURLFORM_COPYNAME, remoteNewFileKey, CURLFORM_COPYCONTENTS, remoteNewFileName, CURLFORM_END);  
        curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, localfile, CURLFORM_END);
        curl_formadd(&post, &last, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

下半部:

                curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); // upload file size--- Content-Length: size

        curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, 20*1000);    // speed limit

        // verbal -- for debug                
        // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);                
        // curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);                    
                // curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);                        
                
                // progress callback                
        //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);                
        //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cbProgress);
        res = curl_easy_perform(curl);
        if(res != 0){
            ERROR("err string %s", curl_easy_strerror(res));                
        }
        curl_easy_cleanup(curl);   
    } 
    fclose(fp);
    return 0;
}

libCurl的檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.