public FtpWebResponse GetResponse(string method, string path) { Uri uri = new Uri(UnixPathUtils.Combine(_uri, path)); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); request.Method = method; request.Credentials = _credentials; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); return(response); }
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); } }