通过libcurl下载图像 [英] Downloading image via libcurl

查看:178
本文介绍了通过libcurl下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过libcurl下载图像(我知道其他下载选项,但我需要通过libcurl完成)

I try to download image via libcurl (I know other options to download, but I need to get it done via libcurl)

当我下载&保存图像,我无法打开它. 文件大小与自己下载文件时不同.

When I download & save the image, I can not open it. The file size is different as when downloading file myself.

using System;
using System.Windows.Forms;
using SeasideResearch.LibCurlNet;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            HTTP cURL = new HTTP();
            cURL.CurlInit();

            // Getting Data - Downloading the picture
            String data = cURL.HTTPGet("http://www.hcs.harvard.edu/csharp/Logo1.png");

            // Saving Picture
            HTTP.save_file("bilde2.jpg", data);
        }
    }

    class HTTP
    {
        public Easy easy;
        public string SockBuff;

        public void CurlInit()
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
        }

        public string HTTPGet(string URL)
        {
            easy = new Easy();

            Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
            easy.SetOpt(CURLoption.CURLOPT_URL, URL);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            //easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, f);
            easy.Perform();
            return SockBuff;
        }


        public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf);
            return size * nmemb;
        }

        static public void save_file(string file_name, string text_to_write)
        {
            using (FileStream stream = new FileStream(file_name, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    //writer.Write("hello");
                    writer.Write(text_to_write);
                    writer.Close();
                }
            }
        }

    }
}

我在这里做错了什么?

What am I doing wrong here ?

推荐答案

每次调用函数save_file时,您的代码都会重新创建该文件.

Every time the function save_file is called, the file is recreated by your code.

因此,您应该检查文件是否存在.

So, you should check the file is exists or not.

如果文件存在,则FileStream应该将FileMode与Append一起使用.

If the file is exists, FileStream should use the FileMode with Append.

尝试以下代码:

static public void save_file(string file_name, string text_to_write)
{
  using (var stream = File.Exists(file_name) 
    ? new FileStream(file_name, FileMode.Append) 
    : new FileStream(file_name, FileMode.Create))
  {
    using (var writer = new BinaryWriter(stream))
    {
      //writer.Write("hello");
      writer.Write(text_to_write);
      writer.Close();
    }
  }
}

这篇关于通过libcurl下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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