Uploading a file using libcurl.net
November 1, 2009 by Nauman · 3 Comments
Following is the code to upload a file to a ftp server through libCurl.NET library.
using System;
usingSystem.IO;
usingSeasideResearch.LibCurlNet;
class Upload
{
public static void Main( String [] args)
{
try
{
Curl .GlobalInit(( int ) CURLinitFlag .CURL_GLOBAL_ALL);
FileStream fs = new FileStream ( "D:/123.txt" , FileMode .Open, FileAccess .Read, FileShare .Read);
Easy easy = new Easy ();
Easy . ReadFunction rf = new Easy . ReadFunction (OnReadData);easy.SetOpt(
CURLoption .CURLOPT_READFUNCTION, rf);
easy.SetOpt(CURLoption .CURLOPT_READDATA, fs);
Easy . DebugFunction df = new Easy . DebugFunction (OnDebug);
easy.SetOpt(CURLoption .CURLOPT_DEBUGFUNCTION, df);easy.SetOpt(
CURLoption .CURLOPT_VERBOSE, true );
Easy . ProgressFunction pf = new Easy . ProgressFunction (OnProgress);easy.SetOpt(
CURLoption .CURLOPT_PROGRESSFUNCTION, pf);
easy.SetOpt(CURLoption .CURLOPT_URL, "FTP Address" + Path .GetFileName( "D:/123.txt" ));easy.SetOpt(
CURLoption .CURLOPT_USERPWD, "Username" + ":" + "Password" );
easy.SetOpt(CURLoption .CURLOPT_UPLOAD, true );easy.SetOpt(
CURLoption .CURLOPT_INFILESIZE, fs.Length);
easy.Perform();
easy.Cleanup();
fs.Close();
Curl .GlobalCleanup();
}
catch ( Exception ex)
{
Console .WriteLine(ex);
}
}
public static Int32 OnReadData( Byte [] buf, Int32 size, Int32 nmemb, Object extraData)
{
FileStream fs = ( FileStream )extraData;
return fs.Read(buf, 0, size * nmemb);
}
public static void OnDebug( CURLINFOTYPE infoType, String msg, Object extraData)
{
Console .WriteLine(msg);
}
public static Int32 OnProgress( Object extraData, Double dlTotal, Double dlNow, Double ulTotal, Double ulNow)
{
Console .WriteLine( "Progress: {0} {1} {2} {3}" ,
dlTotal, dlNow, ulTotal, ulNow);
return 0; // standard return from PROGRESSFUNCTION
}
}
Be sure to add the libCurl.net dll in the bin folder.
Very nce post! thanks for sharing….
..would you have a link to a site on how to install libcurl to use with vs.net? Ive googled with liike more that 8 year old documents.
thanks for the great post!
Dave.
Just want to say what a great blog you got here!
I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
Thumbs up, and keep it going!
Cheers
Christian
Incredibly great writing! Really!