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

家住魔仙堡 提交于 2019-12-23 13:19:34

问题


I would like to send pictures via a program written in C + +. - OK It works, but I would like to send the 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)


回答1:


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



回答2:


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?



来源:https://stackoverflow.com/questions/2529357/sending-a-file-from-memory-rather-than-disk-over-http-using-libcurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!