Topic "report progress"

Author Message
noxide
[View user's profile]

Joined: 2012-05-07
Posts: 3
Is there anyway to report progress to a background worker with session.GetFiles?

Thank you.
Advertisements
noxide
[View user's profile]

Joined: 2012-05-07
Posts: 3
Sorry I didnt enter any code, but I didnt have any to enter using winscp, read through docs and google but couldnt find a way. I'd like to use winscp to report progress like shown below:
Code:
FileStream stream = File.OpenRead(filePath);
                byte[] buffer = new byte[2048];
                int contentLen = stream.Read(buffer, 0, buffer.Length);
                int totalReadBytesCount = contentLen;

                Stream reqStream = request.GetRequestStream();
                while (contentLen != 0)
                {
                    reqStream.Write(buffer, 0, buffer.Length);
                    contentLen = stream.Read(buffer, 0, buffer.Length);
                    totalReadBytesCount += contentLen;
                    var progress = totalReadBytesCount * 100.0 / stream.Length;
                    bw.ReportProgress((int)progress);
                }
[/code]
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Thanks for your post.

This request is being tracked already:
https://winscp.net/tracker/show_bug.cgi?id=818
_________________
Martin Prikryl
noxide
[View user's profile]

Joined: 2012-05-07
Posts: 3
Ok cool, thank you, I got around the limitation by doing this:
Code:
 
 public static string FtpFilesSecurely(string file, string host, string direction, BackgroundWorker bw)
        {
                            var fileInfo = session.GetFileInfo(file);
                            //CalculateDownload
                            worker1.DoWork += (sender, e) =>
                                e.Result = worker1_DoWork(fileInfo, FTP, bw, file);
                            worker1.RunWorkerAsync();

                            //Download files
                            TransferOperationResult transferResult =
                                session.GetFiles(file, FTP.sourceDirectory, false, transferOptions);

                            // Throw on any error
                            transferResult.Check();

                            // Print results
                            return transferResult.IsSuccess ? "Download Succeeded." : "Download Failed.";
}
public static object worker1_DoWork(RemoteFileInfo fileInfo, CompanyFtpInfo FTP, BackgroundWorker bw, string file)
        {
            const string filePart = ".filepart";
            file = FTP.sourceDirectory + file + filePart;
            while (!File.Exists(file))
            {
                System.Threading.Thread.Sleep(1000);
            }
            CalculateDownload(fileInfo, bw, file);
            return "Calculation Complete.";
        }
private static void CalculateDownload(RemoteFileInfo fileInfo, BackgroundWorker bw, string file)
        {
            long remoteFileSize = fileInfo.Length;
            var localFile = new FileInfo(file);
                long localFileSize = localFile.Length;
                while (localFileSize < remoteFileSize)
                {
                    long progress = localFileSize*100/remoteFileSize;
                    bw.ReportProgress((int) progress);

                    localFile = new FileInfo(file);
                    localFileSize = localFile.Length;
                   
                    //this works because as a file is downloaded it is called filename.ext.filepart
                    //when the file is done downloading it is then called filename.ext so therefore
                    //the original name doesnt exist anymore.
                    if (!File.Exists(file))
                        break;
                }
        }


It doesn't work 100% but works pretty good, an advanced programmer could probably make it work better.
VictorShumiF1
[View user's profile]

Joined: 2012-03-15
Posts: 5
Location: Russia
+1
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License