发送图像到ftp从gchar缓冲服务器(libcurl的) [英] Sending an image to ftp server from gchar buffer (libcurl)

查看:227
本文介绍了发送图像到ftp从gchar缓冲服务器(libcurl的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用C哪些进程GDK pixbuf的图像,然后通过FTP(libcurl的)应该将它们发送到远程服务器编程的Linux应用。图像被保存到用的 gdk_pixbuf_save_to_buffer

I'm developing a linux app programmed in C which processes gdk pixbuf images and then should send them to a remote server via ftp (libcurl). The images are saved into a gchar buffer with gdk_pixbuf_save_to_buffer.

问题是我不知道如何结合的libcurl的阅读回调函数正确发送图像到远程服务器。我所有的努力,到目前为止已产生随机字节到生成的文件。

The problem is I don't know how to use the data in this buffer in conjunction with libcurl read callback function to send the image properly to the remote server. All my attempts so far have produced random bytes into the resulting file.

还有就是阅读libcurl的回调函数,它看起来像这样的例子:

There is an example of the libcurl read callback function which looks like this:

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
    size_t retcode = fread(ptr, size, nmemb, stream);
    return retcode;
} 

我需要做我自己的回调函数获取gchar缓冲区作为输入,而不是文件流,并读取尺寸* nmemb个字节从它并将其存储到* PTR。

I would need to do my own callback function that gets the gchar buffer as input instead of file stream and reads size*nmemb bytes from it and stores it to *ptr.

这关系到我的<一个href=\"http://stackoverflow.com/questions/9370468/saving-an-image-jpg-on-a-remote-server-without-local-hard-disk\">$p$pvious问题。

推荐答案

read_callback()是卷曲的呼叫时,它需要获得将要上传的数据一个功能到服务器。试想一下, read_callback()类似的东西 FREAD()。当把它叫做执行所需要的任何巫毒天书,但最终上载的数据必须存储在 * PTR 缓冲,这是袅袅的内部缓冲区。为了您的内存缓冲区的memcpy()会做得很好的身体 read_callback(),所以你不吨需要真正的 FREAD()的。

The read_callback() is a function which CURL calls when it need to obtain data that will be uploaded to the server. Imagine that read_callback() is something similar to fread(). When called it performs any voodoo-mumbo-jumbo that is needed, but in the end uploadable data must be stored in *ptr buffer, which is curl's internal buffer. For your in-memory buffer memcpy() will do just fine as body of read_callback(), so you don't need real fread() at all.

尺寸* nmemb个告诉你有多大的缓冲卷曲已预留了数据的单个块。最后一个无效* 是由CURLOPT_READDATA选项设置一个指针 - 这是一个做什么,永远你-需要与 - 它那种指针,所以它可以点到您要上传包含数据结构和一些附加信息如:目前的进展。

size * nmemb tells you how big buffer curl has reserved for a single chunk of data. The last void* is a pointer which was set by CURLOPT_READDATA option - it's a do-what-ever-you-need-with-it kind of pointer, so it can point to a structure containing data which you're uploading and a some additional info e.g. current progress.

您可以使用此作为样本:

You may use this as a sample:

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>

struct transfer
{
    gchar *buf;
    gsize total;
    size_t uploaded;
};

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
    struct transfer * tr = data;
    size_t left = tr->total - tr->uploaded;
    size_t max_chunk = size * nmemb;
    size_t retcode = left < max_chunk ? left : max_chunk;

    memcpy(ptr, tr->buf + tr->uploaded, retcode); // <-- voodoo-mumbo-jumbo :-)

    tr->uploaded += retcode;  // <-- save progress
    return retcode;
} 

int main()
{
    GdkPixbuf * gbuffer = NULL;
    GError * error = NULL;
    gchar * buffer;
    gsize size;
    g_type_init();
    gbuffer = gdk_pixbuf_new_from_file("g.png", &error);
    gdk_pixbuf_save_to_buffer(gbuffer, &buffer, &size, "jpeg", &error, NULL);

    struct transfer tr = {buffer, size, 0};

    CURL *easyhandle = curl_easy_init();
    curl_easy_setopt(easyhandle, CURLOPT_READFUNCTION, read_callback); 
    curl_easy_setopt(easyhandle, CURLOPT_READDATA, &tr); // <-- this will be *data in read_callback()
    curl_easy_setopt(easyhandle, CURLOPT_UPLOAD, 1L); 
    curl_easy_setopt(easyhandle, CURLOPT_URL, "http://example.com/upload.php");
    CURLcode rc = curl_easy_perform(easyhandle);
}

这篇关于发送图像到ftp从gchar缓冲服务器(libcurl的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆