1

I have to use CURL FTP to upload data on FTP location. But I am not much aware about CURL Commands. I have search a lot on web but didn't find any working example with C#. I have to create an application in C# using CURL command which can upload file/folder on FTP.

Can anyone provide me some example or useful links which have working example?

Banketeshvar Narayan
  • 3,799
  • 4
  • 38
  • 46
  • .Net has built in classes for this, no need for curl: E.g. [Upload file to ftp using c#](http://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp) – Alex K. May 08 '15 at 14:30
  • yes, But CURL FTP has some extra features like it does not require user interaction.... Also it has been recommended by customer that is why I have to use CURL FTP. – Banketeshvar Narayan May 08 '15 at 14:39
  • Its unlikely any FTP library would *require* user interaction when called from code – Alex K. May 08 '15 at 14:40

2 Answers2

0

You can use Invoke-RestMethod in Powershell as mentioned on superuser.

Community
  • 1
  • 1
LosManos
  • 7,195
  • 6
  • 56
  • 107
0

you can use this code in C#...

using System;
using System.Collections.Generic;
using SeasideResearch.LibCurlNet;

namespace libcurlFtpsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("libcurlFtpsExample...");

            const string url = "ftp://user:thepassword@ftp.somesite.com/dir/";

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy = new Easy();
                if (easy != null)
                {
                    easy.SetOpt(CURLoption.CURLOPT_URL, url);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false);
                    easy.SetOpt(CURLoption.CURLOPT_FTP_SSL, CURLftpSSL.CURLFTPSSL_TRY);

                    // For debugging will print headers to console.
                    Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);
                    easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);

                    // For debugging will print received data to console.
                    Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
                    easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
                    easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);

                    // List directory only
                    easy.SetOpt(CURLoption.CURLOPT_FTPLISTONLY, true);

                    CURLcode code = easy.Perform();
                    if (code != CURLcode.CURLE_OK)
                    {
                        Console.WriteLine("Request failed!");
                    }

                    easy.Cleanup();
                }
                else
                {
                    Console.WriteLine("Failed to get Easy libcurl handle!");
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
            finally
            {
                Curl.GlobalCleanup();
            }
        }

        public static void OnDebug(CURLINFOTYPE infoType, String msg, Object extraData)
        {
            // print out received data only
            if (infoType == CURLINFOTYPE.CURLINFO_DATA_IN)
                Console.WriteLine(msg);
        }

        public static Int32 OnHeaderData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            Console.Write(System.Text.Encoding.UTF8.GetString(buf));
                return size * nmemb;
        }
    }
}

thanks

Mukesh Kumar
  • 2,354
  • 4
  • 26
  • 37