Exemple #1
0
        public FtpMediaStream(IngestMedia media, bool forWrite)
        {
            _media = media;
            if (media == null || media.Directory == null)
            {
                throw new ApplicationException();
            }
            Uri uri = new Uri(media.FullPath);

            _client             = new System.Net.FtpClient.FtpClient();
            _client.Credentials = ((IngestDirectory)media.Directory)._getNetworkCredential();
            _client.Host        = uri.Host;
            try
            {
                _client.Connect();
                if (forWrite)
                {
                    _ftpStream = _client.OpenWrite(uri.LocalPath);
                }
                else
                {
                    _ftpStream = _client.OpenRead(uri.LocalPath);
                }
            }
            catch
            {
                _client.Dispose();
                throw;
            }
        }
Exemple #2
0
        public FtpMediaStream(IngestMedia media, bool forWrite)
        {
            if (media?.Directory == null)
            {
                throw new ApplicationException();
            }
            var uri = new Uri(media.FullPath);

            _client = new System.Net.FtpClient.FtpClient
            {
                Credentials = ((IngestDirectory)media.Directory).GetNetworkCredential(),
                Host        = uri.Host
            };
            try
            {
                _client.Connect();
                _ftpStream = forWrite ?
                             _client.OpenWrite(uri.LocalPath) :
                             _client.OpenRead(uri.LocalPath);
            }
            catch
            {
                _client.Dispose();
                throw;
            }
        }
Exemple #3
0
        private void DownloadBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var ftpClient = new System.Net.FtpClient.FtpClient())
                {
                    var destPath   = RemoteDirectoryTB.Text;
                    var sourcePath = LocalFilenameTB.Text;

                    ftpClient.Host           = HostTB.Text;
                    ftpClient.Credentials    = new System.Net.NetworkCredential(UserTB.Text, PasswordTB.Password);
                    ftpClient.EncryptionMode = System.Net.FtpClient.FtpEncryptionMode.None;
                    ftpClient.Connect();

                    using (var fileStream = File.OpenRead(sourcePath))
                        using (var ftpStream = ftpClient.OpenWrite(string.Format("{0}/{1}", destPath, System.IO.Path.GetFileName(sourcePath))))
                        {
                            var buffer = new byte[8 * 1024];
                            int count;
                            while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                ftpStream.Write(buffer, 0, count);
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                // Typical exceptions here are IOException, SocketException, or a FtpCommandException
                //TODO catch some exceptions here and report them
            }
        }
 /// <summary>
 /// Invia un testo (con codifica Unicode) tramite FTP.
 /// </summary>
 /// <param name="contenuto">Il testo da inviare.</param>
 /// <param name="host"></param>
 /// <param name="usr"></param>
 /// <param name="pwd"></param>
 /// <param name="fileName">Path e nome del file di destinazione.</param>
 private void SendFtp(string contenuto, string host, string usr, string pwd, string fileName)
 {
     // upload di un file tramite ftp
     using (System.Net.FtpClient.FtpClient client = new System.Net.FtpClient.FtpClient()) {
         client.Host        = host;
         client.Credentials = new System.Net.NetworkCredential(usr, pwd);
         client.Connect();
         using (var ftpStream = client.OpenWrite(fileName)) {
             byte[] buffer = System.Text.ASCIIEncoding.Unicode.GetBytes(contenuto);
             ftpStream.Write(buffer, 0, buffer.Length);
         }
     }
 }
        public bool Send(Stream srcStream, string destPath)
        {
            try
            {
                using (var s = ftpClient.OpenWrite(destPath))
                {
                    try
                    {
                        srcStream.CopyTo(s);
                    }
                    finally
                    {
                        s.Close();
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Exemple #6
0
        public override void Execute(Options options, System.Net.FtpClient.FtpClient client)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            foreach (var f in options.File.Split(','))
            {
                var fileName = Path.GetFileName(f);
                if (File.Exists(f))
                {
                    using (Stream ostream = client.OpenWrite(fileName))
                    {
                        try
                        {
                            using (var source = File.OpenRead(f))
                            {
                                source.CopyTo(ostream);
                            }
                        }
                        finally
                        {
                            ostream.Close();
                        }
                    }
                }
                else
                {
                    Console.WriteLine("file [{0}] not exits.", fileName);
                }
            }
        }