Ejemplo n.º 1
0
        public void UploadMultiple(List <Tuple <string, string, string> > filesWithPaths, FileExistsAction fileExistsAction, FTPMultipleTransferProgressChangedEventHandler uploadProgressChanged, FTPTransferProgressEventHandler progressChanged, FTPTransferCanceledEventHandler uploadCanceled, CancellationTokenSource cts)
        {
            try
            {
                for (int i = 1; i <= filesWithPaths.Count; i++)
                {
                    var item = filesWithPaths[i - 1];

                    Upload(item.Item1, item.Item2, item.Item3, fileExistsAction, progressChanged, cts);
                    uploadProgressChanged.Invoke(i);
                }
            }
            catch (WebException exception)
            {
                uploadCanceled.Invoke(exception);
            }
        }
Ejemplo n.º 2
0
 public void Upload(string localPath, string remoteDirectory, string remoteFileName, FileExistsAction fileExistsAction, FTPTransferProgressEventHandler progressChanged, FTPTransferCanceledEventHandler uploadCanceled, CancellationTokenSource cts)
 {
     try
     {
         Upload(localPath, remoteDirectory, remoteFileName, fileExistsAction, progressChanged, cts);
     }
     catch (WebException e)
     {
         uploadCanceled?.Invoke(e);
     }
 }
Ejemplo n.º 3
0
        public byte[] Upload(string localPath, string remoteDirectory, string remoteFileName, FileExistsAction fileExistsAction, FTPTransferProgressEventHandler progressChanged, CancellationTokenSource cts)
        {
            string path = (remoteDirectory + (string.IsNullOrWhiteSpace(remoteFileName) ? "" : "/" + remoteFileName));
            string url  = UnixPathUtils.Combine(_uri.ToString(), path);
            Uri    uri  = new Uri(url);

            if (fileExistsAction == FileExistsAction.Overwrite)
            {
                try
                {
                    Delete(Path.Combine(remoteDirectory, remoteFileName));
                }
                catch { }
            }
            else if (fileExistsAction == FileExistsAction.Skip && FileExists(path))
            {
                return(null);
            }
            using (WebClient client = new WebClient())
            {
                client.Credentials            = _credentials;
                client.UploadProgressChanged += (sender, e) =>
                {
                    double percent = (double)e.BytesSent / (double)e.TotalBytesToSend;
                    double seconds = _stopWatch.ElapsedMilliseconds / 1000;
                    double bps     = 0;
                    if (seconds > 0)
                    {
                        bps = e.BytesSent / seconds;
                    }
                    progressChanged?.Invoke(bps, percent * 100);
                };
                cts?.Token.Register(client.CancelAsync);

                try
                {
                    GetResponse(WebRequestMethods.Ftp.MakeDirectory, remoteDirectory);
                }
                catch { }

                _stopWatch = Stopwatch.StartNew();
                byte[] task = client.UploadFileTaskAsync(uri, "STOR", localPath.ToString()).GetAwaiter().GetResult();
                _stopWatch.Stop();
                return(task);
            }
        }
Ejemplo n.º 4
0
        public void Download(string remotePath, string localPath, SSHClient sshClient, FTPTransferProgressEventHandler progressChanged, FTPTransferCanceledEventHandler downloadCanceled, CancellationTokenSource cts)
        {
            try
            {
                long totalBytes = 0;
                using (var client = new WebClient())
                {
                    totalBytes = sshClient.GetFileSize(sshClient.Root + remotePath);

                    cts?.Token.Register(client.CancelAsync);

                    client.Credentials              = _credentials;
                    client.DownloadProgressChanged += (sender, e) =>
                    {
                        double percentage = (double)e.BytesReceived / (double)totalBytes;
                        percentage *= 100;
                        double seconds = _stopWatch.ElapsedMilliseconds / 1000;
                        double bps     = 0;
                        if (seconds > 0)
                        {
                            bps = e.BytesReceived / seconds;
                        }
                        progressChanged.Invoke(bps, percentage);
                    };

                    if (_uri.ToString().EndsWith("/") && remotePath.StartsWith("/"))
                    {
                        remotePath = remotePath.Length > 1 ? remotePath.Substring(1) : string.Empty;
                    }

                    Uri uri = new Uri($"{_uri.ToString()}{remotePath}");

                    _stopWatch = Stopwatch.StartNew();

                    client.DownloadFileTaskAsync(uri, localPath).GetAwaiter().GetResult();

                    _stopWatch.Stop();
                }
            }
            catch (WebException exception)
            {
                downloadCanceled?.Invoke(exception);
            }

            return;
        }