使用libcurl c ++

时间:2016-03-12 21:13:21

标签: visual-c++ libcurl vc90

你能告诉我写的是什么吗? 三个选项

我在这里写了一个使用libcurl上传文件的功能:

long RequestUploadFile(CkString Url,CkStringArray* filename,CkStringArray* filepaths) {
    CURL *curl;
    CURLcode res;

    char *url = (char*)Url.getString();

    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);

    for (int i = 0; i < filename->get_Length(); i++)
    {
        curl_formadd(&formpost,
            &lastptr,
            CURLFORM_COPYNAME, filename->getString(i),
            CURLFORM_FILE, filepaths->getString(i),
            CURLFORM_END);
    }

    curl_formadd(&formpost,
        &lastptr,
        CURLFORM_COPYNAME, "upload_type",
        CURLFORM_COPYCONTENTS, "files",
        CURLFORM_END);

    long http_code = 0;
    curl = curl_easy_init();
    headerlist = curl_slist_append(headerlist, buf);
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        curl_formfree(formpost);
        curl_slist_free_all(headerlist);
    }
    Url.clear();
    filename->Clear();
    filepaths->Clear();
    return http_code;
}

我在我的代码中使用了这个函数,如下所示上传一个文件:

CkStringArray filenames;
CkStringArray filepaths;

filenames.Append("file");
filepaths.Append("C:\\tss.bmp");

RequestUploadFile(RequestUrl, &filenames, &filepaths);

tss.bmp 的大小为4mb(未上传)

hello.txt 的大小为2-3 kb(已成功上传)

这是什么原因以及如何解决?

0 个答案:

没有答案