protected void OnFileUploading(FileUploadingEventArgs e) { EventHandler <FileUploadingEventArgs> handler = FileUploading; if (handler != null) { handler(this, e); } }
public void transferFile() { //richTextBox1.AppendText("Creating client and connecting"); LogThis("\rCreating client and connecting..."); bool directoryAlreadyExists = false; using (var client = new SftpClient(HostName, _port, UserName, Password)) { try { // connect to SFTP host: client.Connect(); OutputThis(string.Format("\rConnected to {0}", HostName)); // cd to the remote folder we want as the root: client.ChangeDirectory(WorkingDirectory); LogThis(string.Format("\rChanged directory to {0}", WorkingDirectory)); LogThis(string.Format("\rChanged directory to {0}", WorkingDirectory)); // this just lists the current [root] folder contents to make sure we're where we think we are: for testing only //var listDirectory = client.ListDirectory(workingdirectory); //LogThis("\rListing directory:"); //// event args for listing directory event: //ListingDirectoryEventArgs listArgs = new ListingDirectoryEventArgs(); //foreach (var fi in listDirectory) //{ // listArgs.FolderName = workingdirectory; // listArgs.FileName = fi.Name; // OnListingDirectory(listArgs); // LogThis(string.Format("\r{0}", fi.Name)); //} // make the new directory on the server, test if it already exists though: // a bug in client.Exists prevents me from using it reliably, so just try to // cd to the dir and trap for the error if it doesn't exist: //if (client.Exists(workingdirectory + "/" + PhotoFTPFolderName) == false) //{ // client.CreateDirectory(PhotoFTPFolderName); // LogThis("directory created."); //} try { client.ChangeDirectory(WorkingDirectory + "/" + PhotoFTPFolderName); directoryAlreadyExists = true; } catch (Exception exDirError) { if (exDirError != null) { if (exDirError.Message.Contains("not found")) { directoryAlreadyExists = false; } } } if (directoryAlreadyExists == false) { client.CreateDirectory(PhotoFTPFolderName); LogThis("directory created."); } client.ChangeDirectory(WorkingDirectory + "/" + PhotoFTPFolderName); LogThis("set working directory to photo Directory"); LogThis(string.Format("\rset working directory to {0}", WorkingDirectory + "/" + PhotoFTPFolderName)); //listDirectory = client.ListDirectory(workingdirectory); //LogThis("\rListing directory:"); //foreach (var fi in listDirectory) //{ // LogThis(string.Format("\r{0}", fi.Name)); //} using (var fileStream = new FileStream(UploadFileName, FileMode.Open)) { LogThis(string.Format("\rUploading {0} ({1:N0} bytes)", UploadFileName, fileStream.Length)); client.BufferSize = 4 * 1024; // bypass Payload error large files //client.UploadFile(fileStream, Path.GetFileName(uploadfile)); // try async: var asyncResult = client.BeginUploadFile(fileStream, Path.GetFileName(UploadFileName), null, null) as Renci.SshNet.Sftp.SftpUploadAsyncResult; //var asyncResult = async1 as CommandAsyncResult; while (!asyncResult.IsCompleted) { FileUploadingEventArgs args = new FileUploadingEventArgs(); args.BytesSentSoFar = asyncResult.UploadedBytes; args.FileNameInProgress = UploadFileName; args.FileBytesTotal = fileStream.Length; LogThis(string.Format("\rUploaded {0:#########}", (asyncResult.UploadedBytes * 1024))); OnFileUploading(args); Thread.Sleep(200); } LogThis(string.Format("\rUploaded {0:#########}", (asyncResult.UploadedBytes * 1024))); if (asyncResult.IsCompleted) { FileUploadCompletedEventArgs fileCompleteArgs = new FileUploadCompletedEventArgs(); fileCompleteArgs.UploadedFileName = UploadFileName; fileCompleteArgs.BytesSent = asyncResult.UploadedBytes; client.EndUploadFile(asyncResult); OnFileUploadCompleted(fileCompleteArgs); // raise an event to tell the client we're done! //MessageBox.Show("File upload completed!", "File Upload", MessageBoxButtons.OK, MessageBoxIcon.Information); } //MessageBox.Show("File upload completed!", "File Upload", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { if (ex == null) { throw; } else { LogThis(ex.Message); // throw this exception so the consumer knows throw ex; } } finally { client.Disconnect(); } } }
public void transferFiles(string[] filesToUpload) { //richTextBox1.AppendText("Creating client and connecting"); LogThis("\rCreating client and connecting..."); bool directoryAlreadyExists = false; int fileCount = filesToUpload.Count(); int fileCounter = 0; using (var client = new SftpClient(HostName, _port, UserName, Password)) { try { // connect to SFTP host: client.Connect(); OutputThis(string.Format("\rConnected to {0}", HostName)); // cd to the remote folder we want as the root: client.ChangeDirectory(WorkingDirectory); LogThis(string.Format("\rChanged directory to {0}", WorkingDirectory)); LogThis(string.Format("\rChanged directory to {0}", WorkingDirectory)); try { client.ChangeDirectory(WorkingDirectory + "/" + PhotoFTPFolderName); directoryAlreadyExists = true; } catch (Exception exDirError) { if (exDirError != null) { if (exDirError.Message.Contains("not found")) { directoryAlreadyExists = false; } } } if (directoryAlreadyExists == false) { client.CreateDirectory(PhotoFTPFolderName); LogThis("directory created."); } client.ChangeDirectory(WorkingDirectory + "/" + PhotoFTPFolderName); LogThis("set working directory to photo Directory"); LogThis(string.Format("\rset working directory to {0}", WorkingDirectory + "/" + PhotoFTPFolderName)); // manage the uploading of multiple files foreach (var file in filesToUpload) { fileCounter++; using (var fileStream = new FileStream(file, FileMode.Open)) { LogThis(string.Format("\rUploading {0} ({1:N0} bytes)", file, fileStream.Length)); client.BufferSize = 4 * 1024; // bypass Payload error large files //client.UploadFile(fileStream, Path.GetFileName(uploadfile)); // try async: var asyncResult = client.BeginUploadFile(fileStream, Path.GetFileName(file), null, null) as Renci.SshNet.Sftp.SftpUploadAsyncResult; //var asyncResult = async1 as CommandAsyncResult; while (!asyncResult.IsCompleted) { FileUploadingEventArgs args = new FileUploadingEventArgs(); args.BytesSentSoFar = asyncResult.UploadedBytes; args.FileNameInProgress = file; args.FileBytesTotal = fileStream.Length; args.FileNumberOfTotal = fileCounter; args.TotalNumberFilesToTransfer = fileCount; LogThis(string.Format("\rUploaded {0:#########}", (asyncResult.UploadedBytes * 1024))); OnFileUploading(args); Thread.Sleep(200); } LogThis(string.Format("\rUploaded {0:#########}", (asyncResult.UploadedBytes * 1024))); if (asyncResult.IsCompleted) { FileUploadCompletedEventArgs fileCompleteArgs = new FileUploadCompletedEventArgs(); fileCompleteArgs.UploadedFileName = file; fileCompleteArgs.BytesSent = asyncResult.UploadedBytes; client.EndUploadFile(asyncResult); OnFileUploadCompleted(fileCompleteArgs); // raise an event to tell the client we're done! //MessageBox.Show("File upload completed!", "File Upload", MessageBoxButtons.OK, MessageBoxIcon.Information); } //MessageBox.Show("File upload completed!", "File Upload", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { if (ex == null) { throw; } else { LogThis(ex.Message); // throw this exception so the consumer knows throw ex; } } finally { client.Disconnect(); } } }