Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: ssh.net uploading with progress bar

$
0
0
Thanks for contacting me. I hope you realise I am not an author of this library, though.
Your question is a bit off topic, progress bar is a UI control, nothing to do with Sftp.
Anyway, here's how you can get the upload progress, which you can then use with progress bar if you need to.

NOTE: Because of the way progress callback is currently implemented, your progress bar is likely to be jumping back and forth during upload.

You should be able to do something like this:
            using( Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient( "some.host", "some.file", "some.password" ) )
            {
                using( Stream fin = File.OpenRead( "file.in" ) )
                {
                    try
                    {
                        sftp.ConnectionInfo.Timeout = new TimeSpan( 0, 0, 30 );

                        sftp.Connect();

                        long total = fin.Length > 0 ? fin.Length : 1;
                        sftp.UploadFile(
                            fin,
                            "/upload/" + "salam.txt",
                            bytesUploaded =>
                            {
                                int percent = (int)((((double)bytesUploaded) / total) * 100.0 );
                                BeginInvoke( new Action( () => m_textBox1.Text = percent + "%" ) );
                            } );
                        MessageBox.Show( "Uploaded Successful" );

                    }
                    catch
                    {
                        MessageBox.Show( "Uploading Error..." );
                    }
                }
            }
You need to use BeginInvoke because progress callbacks will be called from threadpool threads. I assumed you are developing a Windows Forms application.

I hope this helps.

Sylwester

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>