Esempio n. 1
0
        public bool SaveImageToFTPServer(ImageSource imageSource, string imageLocationMemory, string imageLocationDisk)
        {
            BitmapEncoder encoder = new TiffBitmapEncoder();

            byte[] biteArray = ImageSourceToBytes(encoder, imageSource); // Function returns byte[] csv file

            using (var client = new Renci.SshNet.SftpClient(Host, Port, Username, Password))
            {
                client.Connect();
                if (client.IsConnected)
                {
                    client.ChangeDirectory(SFTPWorkingDirectory);
                    using (var ms = new MemoryStream(biteArray))
                    {
                        client.BufferSize = (uint)ms.Length;      // bypass Payload error large files
                        client.UploadFile(ms, imageLocationDisk); // imageLocationDisk == openFileDialog.FileName
                        client.RenameFile(client.WorkingDirectory + "/" + imageLocationDisk, client.WorkingDirectory + "/" + imageLocationMemory);
                        return(true);
                    }
                }
                else
                {
                    OutputMessage = "Couldn't connect to SFTP server.";
                    return(false);
                }
            }
        }
Esempio n. 2
0
        public void Upload(Stream inStream, Stream outStream, string fileName)
        {
            _client.Connect();

            if (_client.IsConnected)
            {
                _client.BufferSize = 4 * 1024;// bypass Payload error large files
                _client.UploadFile(inStream, fileName);

                if (_client.Exists(fileName))
                {
                    var confirmation = new Confirmation {
                        HostName = _client.ConnectionInfo.Host,
                        FileName = fileName,
                        FileSize = _client.GetAttributes(fileName).Size
                    };
                    var outJson = JToken.FromObject(confirmation).ToString();
                    using (var b = new StreamWriter(outStream, Encoding.UTF8, 1000, true)) {
                        b.Write(outJson);
                        b.Flush();
                    }
                }
            }
            else
            {
                throw new IOException($"Cannot connect to {_client.ConnectionInfo.Host}");
            }
        }
Esempio n. 3
0
 //private const string FTP_SERVER = "210.177.12.144";
 //private const string FTP_USR_ID = "sfhpmprd01";
 //private const string FTP_USR_PW = "M8x72bpaT";
 public static bool FileUploadSFTP(string fileName)
 {
     try
     {
         using (var client = new Renci.SshNet.SftpClient(FTP_SERVER, 22, FTP_USR_ID, FTP_USR_PW))
         {
             client.Connect();
             if (client.IsConnected)
             {
                 using (var fileStream = new FileStream(Path.Combine(Path.GetTempPath(), fileName), FileMode.Open))
                 {
                     client.BufferSize = 4 * 1024; // bypass Payload error large files
                     client.UploadFile(fileStream, fileName);
                     AuditLog.Log("BlueX File is uploaded to FTP successfully. [" + fileName + "]");
                 }
             }
         }
         return(true);
     }
     catch (Exception e)
     {
         AuditLog.Log("Exception Line 109 " + e.Message);
         return(false);
     }
 }
Esempio n. 4
0
 public override bool UploadFile(string localPath, string remoteDest)
 {
     using (FileStream file = File.OpenRead(localPath))
     {
         _client.UploadFile(file, remoteDest);
         return(true);
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Uploads a file to the SFTP server.
        /// </summary>
        /// <param name="sourcePath">The local path of the file to read from on the client.</param>
        /// <param name="destinationPath">The remote path to write to on the server.</param>
        /// <returns>True if the upload was successful otherwise false if the upload failed.</returns>
        public bool UploadFile(string sourcePath, string destinationPath)
        {
            try
            {
                using (var fileStream = File.OpenRead(sourcePath))
                {
                    _sftpClient.UploadFile(fileStream, destinationPath, true);
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 6
0
        public bool Upload(FileInfo localFile, string remoteFileName)
        {
            if (!localFile.Exists)
            {
                throw new Exception(string.Format("The local file does not exist, the file path: {0}", localFile.FullName));
            }

            using (var inputStream = localFile.OpenRead())
                using (var client = new Renci.SshNet.SftpClient(_host, _username, _password))
                {
                    client.Connect();
                    client.UploadFile(inputStream, remoteFileName);
                }

            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// Uploads a file to the SFTP server.
        /// </summary>
        /// <param name="sourcePath">The local path of the file to read from on the client.</param>
        /// <param name="destinationFolderPath">The remote folder path to write to on the server.</param>
        /// <param name="destinationFilename">The remote filename to write to on the server.</param>
        /// <param name="isLinux">Determines if we are connecting to a Linux server (if so then backslashes in the destination folder path are replaced with forward slashes).</param>
        /// <returns>True if the upload was successful otherwise false if the upload failed.</returns>
        public bool UploadFile(string sourcePath, string destinationFolderPath, string destinationFilename, bool isLinux)
        {
            try
            {
                string initialWorkingDirectory = _sftpClient.WorkingDirectory;

                if (!string.IsNullOrEmpty(destinationFolderPath))
                {
                    if (isLinux)
                    {
                        destinationFolderPath = destinationFolderPath.Replace("\\", "/");

                        foreach (string folder in destinationFolderPath.Split('/'))
                        {
                            CreateRemoteDirectory(folder);

                            _sftpClient.ChangeDirectory(folder);
                        }
                    }
                    else
                    {
                        CreateRemoteDirectory(destinationFolderPath);

                        _sftpClient.ChangeDirectory(destinationFolderPath);
                    }
                }

                using (var fileStream = File.OpenRead(sourcePath))
                {
                    _sftpClient.UploadFile(fileStream, destinationFilename, true);
                }

                // We have to make sure that we change directory back to the initial working directory
                // so we avoid creating sub-folders within sub-folders. We always want to start from the root
                // with each call of the UploadFile method.
                _sftpClient.ChangeDirectory(initialWorkingDirectory);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Esempio n. 8
0
        private void SendFIleOverSFTP()
        {
            Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient("119.23.71.29", "root", "Zyp885299");
            sftp.Connect();
            FileInfo fi        = new FileInfo(@"/Users/luweiping/Downloads/nginx-1.12.0-1.el7.ngx.x86_64.rpm");
            var      allLength = fi.Length;

            sftp.UploadFile(new System.IO.FileStream(fi.FullName,
                                                     System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite),
                            "/home/parallels/Downloads/nginx-1.12.0-1.el7.ngx.x86_64.rpm", (pro) =>
            {
                Console.WriteLine((pro * 1.0d / allLength * 1.0d).ToString("P"));
            });
            Console.WriteLine("finished.");
            while (true)
            {
                System.Threading.Thread.Sleep(500);
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Client started");

            var sftpClient = new Renci.SshNet.SftpClient("localhost", 2228, "foo", "pass");
            var sourceRoot = "files/";

            sftpClient.Connect();

            var files = new string[]
            {
                "20MB.zip",
                "50MB.zip"
            };

            var runs = 20;

            for (var i = 0; i < runs; i++)
            {
                Console.WriteLine("Run: " + (i + 1));

                foreach (var file in files)
                {
                    var source = sourceRoot + file;
                    Console.WriteLine("Source: " + source);

                    var dest = "/upload/" + file;
                    Console.WriteLine("Destination: " + dest);

                    using (var stream = File.Open(source, FileMode.Open))
                    {
                        sftpClient.UploadFile(stream, dest, value =>
                        {
                            Console.Write("*");
                        });
                    }

                    Console.WriteLine();
                }
            }
        }
Esempio n. 10
0
        public string SendIt()
        {
            var host    = ConfigurationManager.AppSettings["Host"];
            var zipFile = Factory.GetZipFile();

            Renci.SshNet.SftpClient sftpClient = new Renci.SshNet
                                                 .SftpClient(host,
                                                             ConfigurationManager.AppSettings["FtpUserName"],
                                                             ConfigurationManager.AppSettings["FtpUserPass"]);
            sftpClient.Connect();

            sftpClient
            .ChangeDirectory(ConfigurationManager.AppSettings["WorkingDirectory"]);

            using (var fileStream = new FileStream(zipFile, FileMode.Open))
            {
                sftpClient.BufferSize = 4 * 1024; // bypass Payload error large files
                sftpClient.UploadFile(fileStream, Path.GetFileName(zipFile));
            }

            return(new WebClient()
                   .DownloadString(ConfigurationManager.AppSettings["UpdateDatabaseUrl"]));
        }
Esempio n. 11
0
        private static void RenameAndUploadFilesForConservazione()
        {
            if (!YES_TO_UPLOAD_CONSERVAZIONE)
            {
                return;
            }


            Utils.CLogger.WriteLog("sending per conservazione inizio");

            DirectoryInfo d;

            FileInfo[] Files;

            try
            {
                if (SFTPClient.IsConnected)
                {
                    d     = new DirectoryInfo(LOCAL_UPLOAD_FOLDER_CONSERVAZIONE);
                    Files = d.GetFiles("*.ctrl");

                    foreach (FileInfo file in Files)
                    {
                        try
                        {
                            Utils.CLogger.WriteLog("deleting: " + file.FullName);
                            System.IO.File.Delete(file.FullName);
                            Utils.CLogger.WriteLog(file.FullName + "deleted!!!");
                        }
                        catch (Exception ex)
                        {
                            Utils.CLogger.WriteLog(ex);
                        }
                    }

                    Files = d.GetFiles("*.tmp");
                    string xmlname = "";

                    foreach (FileInfo file in Files)
                    {
                        try
                        {
                            xmlname = file.FullName.Replace(".tmp", ".xml");
                            Utils.CLogger.WriteLog("renaming: " + file.FullName + " to " + xmlname);
                            if (System.IO.File.Exists(file.FullName))
                            {
                                System.IO.File.Move(file.FullName, xmlname);
                            }
                            Utils.CLogger.WriteLog(file.FullName + "renamed!!!");
                        }
                        catch (Exception ex)
                        {
                            Utils.CLogger.WriteLog(ex);
                        }
                    }


                    SFTPClient.ChangeDirectory(SFTP_UPLOAD_FOLDER_CONSERVAZIONE);
                    SFTPClient.BufferSize = 4 * 1024; // bypass Payload error large files

                    Files = d.GetFiles();

                    //destination_full_name = System.IO.Path.Combine(LOCAL_UPLOAD_FOLDER_BACKUP, file.Name);

                    foreach (FileInfo file in Files)
                    {
                        try
                        {
                            Utils.CLogger.WriteLog("uploading: " + file.FullName + " to ");

                            using (var fileStream = new FileStream(file.FullName, FileMode.Open))
                            {
                                Utils.CLogger.WriteLog("Uploading " + file.FullName);
                                SFTPClient.UploadFile(fileStream, file.Name);
                                Utils.CLogger.WriteLog(file.FullName + "Uploaded");
                            }


                            Utils.CLogger.WriteLog(file.FullName + "uploaded !!!");

                            Utils.CLogger.WriteLog("moving " + file.FullName + " to backup folder");
                            System.IO.File.Move(file.FullName, System.IO.Path.Combine(LOCAL_UPLOAD_FOLDER_CONSERVAZIONE_BACKUP, file.Name));
                            Utils.CLogger.WriteLog(file.FullName + " copied");
                        }
                        catch (Exception ex)
                        {
                            Utils.CLogger.WriteLog(ex);
                        }
                    }


                    foreach (FileInfo file in Files)
                    {
                        try
                        {
                        }
                        catch (Exception ex)
                        {
                            Utils.CLogger.WriteLog(ex);
                        }
                    }
                }
                Utils.CLogger.WriteLog("sending per conservazione fine");
            }
            catch (Exception ex)
            {
                Utils.CLogger.WriteLog(ex);
            }
        }