Can someone show me the correct way to download a file given a url using libcurlnet
FileStream fs = new FileStream("something.mp3", FileMode.Create);
WriteData wd = OnWriteData;
easy.SetOpt(CURLoption.CURLOPT_URL, downloadFileUrl);
easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, wd);
if ("CURLE_OK" == easy.Perform().ToString())
{
//save_file
}
static System.Int32 OnWriteData(FileStream stream, byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
stream.Write(buf, 0, buf.Length);
return size * nmemb;
} //OnWriteData
public delegate System.Int32
WriteData(
FileStream stream, //added parameter
byte[] buf, Int32 size, Int32 nmemb, Object extraData);
You don't need to declare your own callback for the WriteFunction, the delegate is declared in
Easy. And you need to pass the delegate to the WriteFunction option, not to the WriteData option. The latter is for the object you want to pass to the WriteFunction delegate. Fixing all those things will give the following working code for me: