HanzUpp3r
HanzUpp3r

Reputation: 51

RestSharp DownloadData sync with progressbar

How can I download file and show downloading progress by ProgressBar in window form app?

RestClient client = new RestClient("http://127.0.0.1/");

RestRequest request = new RestRequest("/test/{FileName}");
request.AddParameter("FileName", "testFile.abc", ParameterType.UrlSegment);

string path = @"C:/Users/[user]/Desktop/testFile.abc";

var fileForDownload = client.DownloadData(request);

fileForDownload.SaveAs(path);

if (File.Exists(@"C:/Users/[user]/Desktop/testFile.abc"))
{
    MessageBox.Show("done");
}

I write somethink like this but I don't know what now?

Upvotes: 2

Views: 4689

Answers (2)

adospace
adospace

Reputation: 2019

I think a better alternative would be to override FileStream to get count of bytes written to file:

string tempFile = Path.Combine(Configuration.DownloadFolder, "TEST.DATA");
using (var writer = new HikFileStream(tempFile))
{
    writer.Progress += (w, e) => {
#if DEBUG
        Console.Write(string.Format("\rProgress: {0} / {1:P2}", writer.CurrentSize, ((double)writer.CurrentSize) / finalFileSize));
#endif
    };
    request.ResponseWriter = (responseStream) => responseStream.CopyTo(writer);
    var response = client.DownloadData(request);
}

where HikFileStream is:

class HikFileStream : FileStream
{
    public HikFileStream(string path)
        : base(path, FileMode.Create, FileAccess.Write, FileShare.None)
    {
    }

    public long CurrentSize { get; private set; }


    public event EventHandler Progress;

    public override void Write(byte[] array, int offset, int count)
    {
        base.Write(array, offset, count);
        CurrentSize += count;
        var h = Progress;
        if (h != null)
            h(this, EventArgs.Empty);//WARN: THIS SHOULD RETURNS ASAP!
    }

}

Upvotes: 6

Sigmund Freud
Sigmund Freud

Reputation: 48

Sorry but you can't, because there is no event handler object in RestClient to take status of download data.

Here is an alternative way to do it;

        //...
        timer1.Interval = 1000; // 1 sec interval.

        timer1.Start();

        RestClient client = new RestClient("http://127.0.0.1/")
                            {
                                Timeout = 10 * 1000 //10 sec timeout time.
                            };

        RestRequest request = new RestRequest("/test/{FileName}");
        request.AddParameter("FileName", "testFile.abc", ParameterType.UrlSegment);

        string path = @"C:/Users/[user]/Desktop/testFile.abc";

        var fileForDownload = client.DownloadData(request);

        fileForDownload.SaveAs(path);

        if (File.Exists(@"C:/Users/[user]/Desktop/testFile.abc"))
        {
            MessageBox.Show("done");
        }

        progressBar1.Value = 100;
        timer1.Stop();
    }

    public void timer1_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value <= 100)
        {
            progressBar1.Value += 10;
        }

        if (progressBar1.Value >= 100)
        {
            progressBar1.Value = 0;
        }
    }

Change the name of "timer1" for naming-best-practices. Good luck...

Upvotes: 0

Related Questions