Libcurl Upload a file, add a custom header

Source: Internet
Author: User

Original http://www.cnblogs.com/meteoric_cry/p/4285881.html ThemeCurl

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

"Accept:text/html, */*;q=0.01"); //... //set headerscurl_easy_setopt (Easyhandle, Curlopt_httpheader, headers); /* Free the header list *   /
Go to official website to view curlopt_httpheader>>

Upload files directly:

struct Curl_httppost *formpost = NULL; struct Curl_httppost *lastptr = NULL; Curl_formadd (&formpost, &lastptr,    "UploadFile",    "/ ",//imagepath    //last free Postcurl_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.htmlcurl_formadd (&post, &last,              curlform_copyname , "File",              Curlform_buffer, "Unnamed.png",              curlform_bufferptr, memblock,                  curlform_bufferlength , Memblock_length,              "Image/png",             curlform_end);  ' file In the 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 Upload a file, add a custom header

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.