Exemplo n.º 1
0
        public string uploadImage(string path, string fileName)
        {
            Chilkat.SFtp sftp = new Chilkat.SFtp();

            bool success;
            success = sftp.UnlockComponent("Anything for 30-day trial");
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            sftp.ConnectTimeoutMs = 5000;
            sftp.IdleTimeoutMs = 10000;

            success = sftp.Connect("http://kdspykim2.cafe24.com", 22);
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            success = sftp.AuthenticatePw("image", "dlalwlfmfsjgdjfk");
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            success = sftp.InitializeSftp();
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            string uploadFileName = makeFileName(fileName);

            string handle;
            handle = sftp.OpenFile("public_html/" + uploadFileName, "writeOnly", "createTruncate");
            if (handle == null)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            success = sftp.UploadFile(handle, path);
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            success = sftp.CloseHandle(handle);
            if (success != true)
            {
                MessageBox.Show(sftp.LastErrorText);
                return null;
            }

            return uploadFileName;
        }
Exemplo n.º 2
0
 public override void Close()
 {
     base.Close();
     _sftp.CloseHandle(_handle);
 }
Exemplo n.º 3
0
        static List <string> GetFileNamesInFtp(List <string> theseDates)
        {
            Dictionary <string, int> FilesAndSizes = new Dictionary <string, int>();
            List <string>            result        = new List <string>();
            List <Chilkat.SFtpFile>  files         = new List <Chilkat.SFtpFile>();

            Chilkat.SFtp sftp = initSftp();
            bool         success;

            //  Open a directory on the server...
            //  Paths starting with a slash are "absolute", and are relative
            //  to the root of the file system. Names starting with any other
            //  character are relative to the user's default directory (home directory).
            //  A path component of ".." refers to the parent directory,
            //  and "." refers to the current directory.
            string handle;

            //handle = sftp.OpenDir("/Incoming/FromETP/History");
            handle = sftp.OpenDir("/Incoming/FromETP/AdditionalHistoryBenchmark");
            if (sftp.LastMethodSuccess != true)
            {
                Console.WriteLine(sftp.LastErrorText);
                return(result);
            }

            //  Download the directory listing:
            Chilkat.SFtpDir dirListing = null;
            dirListing = sftp.ReadDir(handle);
            if (sftp.LastMethodSuccess != true)
            {
                Console.WriteLine(sftp.LastErrorText);
                return(result);
            }
            int i;
            int n = dirListing.NumFilesAndDirs;

            if (n == 0)
            {
                Console.WriteLine("No entries found in this directory.");
            }
            else
            {
                int count = 0;
                for (i = 0; i <= n - 1; i++)
                {
                    Chilkat.SFtpFile fileObj = null;
                    fileObj = dirListing.GetFileObject(i);
                    FilesAndSizes.Add(fileObj.Filename, fileObj.Size32);
                    files.Add(fileObj);

                    count = i;
                }
                Console.WriteLine("found {0} Candidate files", count);
                count = 0;
                foreach (string date in theseDates)
                {
                    int    trytimes   = 0;
                    string searchDate = date;
                    string sFileName  = String.Empty;
                    do
                    {
                        trytimes++;
                        if (trytimes > 10)
                        {
                            log("Error, didn't find for " + searchDate);
                            break;                             // didn't find one for this month
                        }
                        var Ifiles = FilesAndSizes.Where(pv =>
                                                         pv.Key.Contains(searchDate)).Select(pv => pv.Key);
                        if (Ifiles.Count() == 0)
                        {
                            searchDate = PreviousDay(searchDate);
                            continue;
                        }
                        if (Ifiles.Count() > 1)
                        {
                            throw new Exception("more than one file found");
                        }

                        sFileName = Ifiles.FirstOrDefault();
                        if (FilesAndSizes[sFileName] < 4000)
                        {
                            searchDate = PreviousDay(searchDate);
                            sFileName  = String.Empty;
                            continue;
                        }
                        result.Add(sFileName);
                        count++;
                    }while(sFileName == String.Empty);
                }

                Console.WriteLine("found {0} files", count);
            }

            //  Close the directory
            success = sftp.CloseHandle(handle);
            if (success != true)
            {
                Console.WriteLine(sftp.LastErrorText);
                return(result);
            }

            Console.WriteLine("Success.");
            sftp.Disconnect();
            return(result);
        }