Libcurl Uploading Files

Source: Internet
Author: User

Libcurl a lot of parameters, accidentally easy to encounter problems. Once encountered a very egg pain problem: Libcurl Breakpoint Download >>

Here the main summary, Libcurl upload two ways:

1, directly upload the file, similar form form <input type= "file"/>,<form enctype= "Multipart/form-data" ... ;

2, upload binary stream;

As for setting a custom header, use the same method

structCurl_slist *headers=null;
headers = curl_slist_append (headers,"Content-type:text/xml");
headers = curl_slist_append (headers,"accept:text/html, */*;q=0.01");
//...

//set Headers
Curl_easy_setopt (Easyhandle, Curlopt_httpheader, headers);


//last Free the header list
Curl_slist_free_all (headers);/* Free the header list * /

Go to official website to view curlopt_httpheader>>

Upload files directly:

structCurl_httppost *formpost = NULL;
structCurl_httppost *lastptr = NULL;

Curl_formadd (&formpost, &lastptr,
Curlform_copyname,"UploadFile",
Curlform_file,"/",//imagepath
Curlform_contenttype,"Image/jpeg",
Curlform_end);

Curl_formadd (&formpost, &lastptr,
Curlform_copyname,"FileName",
Curlform_copycontents,"Test.jpg",
Curlform_end);

Curl_easy_setopt (M_curl,curlopt_httppost,formpost);

//last free Post
Curl_formfree (Formpost);

If you upload additional file types, add the Curlform_contenttype parameter (content-type/mime-type) to the Curl_formadd
Binary Stream upload:
//referer http://curl.haxx.se/mail/lib-2003-08/0190.html
Curl_formadd (&post, &last,
Curlform_copyname,"File",
Curlform_buffer,"Unnamed.png",
Curlform_bufferptr, Memblock,
Curlform_bufferlength, Memblock_length,
Curlform_contenttype,"Image/png",
Curlform_end);


' file ' is the name of the this part, 'Unnamed.png ' isThe name setinchThe file name field.

None of the above parameters can be defaulted, if the default result may not be as expected. where Content-type defaults to "Application/octet-stream"

Curlform_copyname is the name of the field being uploaded, as shown in (after name)

Curlform_buffer is used when uploading a custom file without using Curlform_file, which is used to tell Libcurl that the contents of the file are already in the cache, and that it provides the filename field in the header information of the content. I didn't add this parameter before I found the upload was unsuccessful--

There is one more place to note: Curlform_bufferlength It must be of type long

To facilitate testing, I used the formidable of node. js to debug

Reference Links:

Http://curl.haxx.se/libcurl/c/curl_formadd.html

Http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

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

Http://stackoverflow.com/questions/14685196/c-libcurl-force-content-type

Http://zengrong.net/post/2088.htm

Http://stackoverflow.com/questions/25370991/libcurl-buffered-file-upload-not-working

Uploading files via HTTP protocol

Using Libcurl post data and uploading files

Libcurl Uploading Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.