2010-04-17 70 views
1

我有一個文件指針文件指針,如下面的文件發佈:我如何捲曲使用C++

FILE* f = tmpfile() 

如何使用libcurl中做一個HTTP POST到一個URL作爲命名場F1?

我試圖讀取該文件內容到一個char *數組,但並用以下上傳:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

#include <curl/curl.h> 
#include <curl/types.h> 
#include <curl/easy.h> 

char* dump_buffer(void *buffer, int buffer_size){ 
int i; 
char *ch = malloc(buffer_size); 
for(i = 0;i < buffer_size;++i){ 
    ch[i] = ((char *)buffer)[i]; 
    //printf("%c",((char *)buffer)[i]); 
} 
return ch; 
} 

char* readFileBytes(const char *name){ 
FILE *file; 
char *buffer; 
unsigned long fileLen; 
int i; 
file = fopen("index.tar", "rb"); 
if (!file) 
{ 
    fprintf(stderr, "can't open file %s", "1.m4v"); 
    exit(1); 
} 

fseek(file, 0, SEEK_END); 
fileLen=ftell(file); 
fseek(file, 0, SEEK_SET); 

buffer=(char *)malloc(fileLen+1); 

if (!buffer) 
{ 
    fprintf(stderr, "Memory error!"); 
    fclose(file); 
    exit(1); 
} 

fread(buffer, fileLen, 1, file); 
fclose(file); 
char* ret = dump_buffer(&buffer, fileLen); 

for(i = 0;i < fileLen;++i){ 
    //printf("%c",ret[i]); 
} 

return ret; 
} 


int main(int argc, char *argv[]) 
{ 
    CURL *curl; 
    CURLcode res; 

    struct curl_httppost *formpost=NULL; 
    struct curl_httppost *lastptr=NULL; 
struct curl_slist *headers=NULL; 
headers = curl_slist_append(headers, "Content-Type: multipart/form-data"); 

    curl_global_init(CURL_GLOBAL_ALL); 

    /* Fill in the filename field */ 

char* p = readFileBytes("index.tar"); 

curl_formadd(&formpost, 
      &lastptr, 
      CURLFORM_COPYNAME, "F2", 
      CURLFORM_FILE, "index.tar", 
      CURLFORM_END); 

curl_formadd(&formpost, 
      &lastptr, 
      CURLFORM_COPYNAME, "F1", 
      CURLFORM_COPYCONTENTS, (char*)p, 
      CURLFORM_END); 
curl = curl_easy_init(); 
    /* initalize custom header list (stating that Expect: 100-continue is not 
wanted */ 
    if(curl) { 
/* what URL that receives this POST */ 
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 
curl_easy_setopt(curl, CURLOPT_URL, "http://oceanfizz.usc.edu/upload.php"); 
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); 
res = curl_easy_perform(curl); 

/* always cleanup */ 
curl_easy_cleanup(curl); 

/* then cleanup the formpost chain */ 
curl_formfree(formpost); 
/* free slist */ 
    } 
    return 0; 
} 

,我得到的是

guest-wireless-207-151-246-070:Desktop ankurcha$ ./postit2 
* About to connect() to oceanfizz.usc.edu port 80 (#0) 
* Trying 128.125.49.29... * connected 
* Connected to oceanfizz.usc.edu (128.125.49.29) port 80 (#0) 
> POST /upload.php HTTP/1.1 
Host: oceanfizz.usc.edu 
Accept: */* 
Content-Length: 20770 
Expect: 100-continue 
Content-Type: multipart/form-data; boundary=----------------------------e04b6194f620 

< HTTP/1.1 100 Continue 
< HTTP/1.1 200 OK 
< Date: Mon, 19 Apr 2010 03:51:04 GMT 
< Server: Apache/2.2.12 (Ubuntu) 
< X-Powered-By: PHP/5.2.10-2ubuntu6.4 
< Vary: Accept-Encoding 
< Content-Length: 335 
< Content-Type: text/html 
< 
array(1) { 
    ["F2"]=> 
    array(5) { 
["name"]=> 
string(9) "index.tar" 
["type"]=> 
string(24) "application/octet-stream" 
["tmp_name"]=> 
string(14) "/tmp/phpyOiqXh" 
["error"]=> 
int(0) 
["size"]=> 
int(20480) 
    } 
} 
array(1) { 
    ["F1"]=> 
    string(0) "" 
} 
Sorry, there was a problem uploading your file. 
* Connection #0 to host oceanfizz.usc.edu left intact 
* Closing connection #0 

我期待F1有二進制輸出內容。

+0

有沒有這樣的事情作爲「文件標識符」。它是一個文件*指針*。 – 2010-04-17 05:32:27

+0

你能解釋一下你想要發佈的內容嗎?文件的內容?文件的名稱? – 2010-04-17 07:24:21

+0

該文件是一個MP4視頻文件。上述命令用於創建該文件。 – 2010-04-18 22:34:59

回答

4

HTTP POST可以通過很多方式完成,因此除非您在問題中指定更多詳細信息,否則沒有單個答案。做POST程序與libcurl中的一種方式,如圖所示:

http://curl.haxx.se/libcurl/c/post-callback.html

如果您更希望做一個多formpost上傳,可能是一個更好的例子是這樣的:

http://curl.haxx.se/libcurl/c/postit2.html

+0

多部分示例使用文件名,但我唯一擁有的是文件指針。那麼,還有另一種方法可以做到這一點嗎?還是第一個唯一的方法? – 2010-04-18 22:37:17

+0

您也可以使用CURLFORM_STREAM或CURLFORM_BUFFER,全部取決於您的偏好。 – 2010-04-20 20:27:33

+0

CURLFORM_BUFFER似乎有竅門! – 2010-05-13 04:51:38

0

你上傳的代碼工作正常。上傳的文件存儲在臨時文件/ tmp/phpyOiqXh中。 PHP不會將上傳文件的內容存儲到變量中,只需存儲臨時文件的路徑即可。