简体   繁体   English

使用libcurl通过HTTP从内存(而不是磁盘)发送文件

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

I would like to send pictures via a program written in C + +. 我想通过用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. - 确定它有效,但我想将预先加载的载体中的图片发送到变量char(你知道我的意思吗?首先,我将图片加载到变量然后发送变量),因为现在我有了指定磁盘上图片的路径。

I wanted to write this program in c++ by using the curl library, not through exe. 我想用c ++编写这个程序,使用curl库而不是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. CURLFORM_PTRCONTENTS在这里不正确,它不会创建文件上传部分。

Instead one should use CURLFORM_BUFFER to send an image from an already existing buffer in memory. 相反,应该使用CURLFORM_BUFFER从内存中已存在的缓冲区发送图像。

 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 阅读curl_formadd的文档: httpcurl_formadd

Specifically, under "Options": 具体来说,在“选项”下:

CURLFORM_PTRCONTENTS 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. libcurl将使用指针并引用应用程序中的数据,因此您必须确保它保持不变直到curl不再需要它。 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. 如果数据不是NUL终止的,或者您希望它包含零字节,则必须使用CURLFORM_CONTENTSLENGTH设置其长度。

CURLFORM_CONTENTSLENGTH CURLFORM_CONTENTSLENGTH

followed by a long giving the length of the contents. 然后是长时间给出内容的长度。 Note that for CURLFORM_STREAM contents, this option is mandatory. 请注意,对于CURLFORM_STREAM内容,此选项是必需的。

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? 我假设您知道如何创建p_jpg_data并将数据读入其中,或者您是否需要解释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM