tags:

views:

53

answers:

2

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 ?

A: 

Strings contain text, which have a given encoding (UTF8 in your case), you can't treat a .png file as being utf-8 encoded text.

Rewrite your HTTPGet and save_file methods to only deal with bytes/byte arrays.

nos
How can I do it?
I tried to code it to not use UTF8, but did not work. Need way around.
How can I save Byte[] buf ?
@user444434 did not work isn't very helpful, post your new code. FileStream has a Write method to write a byte array: http://msdn.microsoft.com/en-us/library/system.io.filestream.write.aspx
nos
I am not even sure what I am supposed to do. My code remains the same except that I replaced: SockBuff = SockBuff + System.Text.Encoding.UTF8.GetString(buf); with SockBuff = SockBuff + buf;
A: 

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

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

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

Try the codes below:

    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();
            }
        }
    }
workingbird