Libcurl File Upload

Source: Internet
Author: User

Recently in the need to use the Upload function curl, using libcurl to achieve. Therefore, use the Curl command first and then use the Libcurl implementation.

The standard method of file upload based on HTTP protocol is: File upload RFC1867 based on post form.

This method is very widely used, this RFC provides a standard way to upload files, as described below, based on Libcurl to develop the upload function.

Development implementation Process

1. Execute code using the Curl command line, 2. Track and analyze Curl's request and response, 3. Using the Libcurl API for development implementation

0. Build Upload Server

---this please google some webserver construction, has been referring to RFC1867 or online examples to choose any language, after writing the network too processing code.

I wrote a upload processing page using Python's Django framework,

Fields that require a form

Title-newfilenameinserver

[Email protected]/abc/

URL http://192.168.0.61/due/upload/

1. Commands to upload files using curl

Curl-f "Title=dddd.txt"-F "[Email protected]/home/chenglun/upload/abc" http://192.168.0.61/due/upload/

2. Analysis

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, bytes (0x17)

http/1.1-Continue.

= = Send data, 249 bytes (0XF9)

Bound ...

Content-disposition:form-data; Name ....

....

Bound ...

<= Recv Header, bytes (0x11)

http/1.1-OK.

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

As you can see from the trace, you only need to building a form, add the corresponding Key=value section, and then use Curl's request to implement the above transmission process.

3. Using Libcurl to implement

The Usage API is: Curl_formadd (); Curl_formfree generates a from and then uses curl_easy_setopt (curl, curlopt_httppost, post), sets curl, and executes.

Curl_formadd () Use references, libcurl documents, documentation for example.

Top half of the code:

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

Bottom half:

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 File Upload

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.