开发者

Sending a file from memory (rather than disk) over HTTP using libcurl

开发者 https://www.devze.com 2022-12-24 16:24 出处:网络
I would like to send pictures via a program written in C + +. - OK It works, but I would like to send th开发者_高级运维e pictures from pre-loaded carrier to a variable char (you know what I mean? Firs

I would like to send pictures via a program written in C + +. - OK It works, but I would like to send th开发者_高级运维e pictures from pre-loaded carrier to a variable char (you know what I mean? First off, I load the pictures into a variable and then send the variable), cause now I have to specify the path of the picture on a disk.

I wanted to write this program in c++ by using the curl library, not through exe. extension. I have also found such a program (which has been modified by me a bit)


CURLFORM_PTRCONTENTS is not the correct usage here, it will not create a file upload part.

Instead one should use CURLFORM_BUFFER to send an image from an already existing buffer in memory.

 curl_formadd(&formpost,
          &lastptr,
          CURLFORM_COPYNAME, "send",
          CURLFORM_BUFFER, "nowy.jpg",
          CURLFORM_BUFFERPTR, data,
          CURLFORM_BUFFERLENGTH, size,
          CURLFORM_END);


Read the documentation for curl_formadd: http://curl.haxx.se/libcurl/c/curl_formadd.html

Specifically, under "Options":

CURLFORM_PTRCONTENTS

followed by a pointer to the contents of this part, the actual data to send away. libcurl will use the pointer and refer to the data in your application, so you must make sure it remains until curl no longer needs it. If the data isn't NUL-terminated, or if you'd like it to contain zero bytes, you must set its length with CURLFORM_CONTENTSLENGTH.

CURLFORM_CONTENTSLENGTH

followed by a long giving the length of the contents. Note that for CURLFORM_STREAM contents, this option is mandatory.

So instead of

 curl_formadd(&formpost,
              &lastptr,
              CURLFORM_COPYNAME, "send",
              CURLFORM_FILE, "nowy.jpg",
              CURLFORM_END);

You'd want something like

 curl_formadd(&formpost,
              &lastptr,
              CURLFORM_COPYNAME, "send",
              CURLFORM_PTRCONTENTS, p_jpg_data,
              CURLFORM_CONTENTSLENGTH, jpg_data_len,
              CURLFORM_END);

I'm assuming you know how to create p_jpg_data and read the data into it, or do you need that explained?

0

精彩评论

暂无评论...
验证码 换一张
取 消