Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RestSharp下载数据与progressbar同步_C#_Restsharp - Fatal编程技术网

C# RestSharp下载数据与progressbar同步

C# RestSharp下载数据与progressbar同步,c#,restsharp,C#,Restsharp,如何在Windows窗体应用程序中通过ProgressBar下载文件并显示下载进度 RestClient client = new RestClient("http://127.0.0.1/"); RestRequest request = new RestRequest("/test/{FileName}"); request.AddParameter("FileName", "testFile.abc", ParameterType.UrlSegment); string path =

如何在Windows窗体应用程序中通过ProgressBar下载文件并显示下载进度

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");
}

我写了一些这样的想法,但我现在不知道该怎么办?

很抱歉,您不能,因为RestClient中没有事件处理程序对象来获取下载数据的状态

这里有另一种方法

        //...
        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;
        }
    }
/。。。
计时器1.间隔=1000;//间隔1秒。
timer1.Start();
RestClient=新的RestClient(“http://127.0.0.1/")
{
超时=10*1000//10秒超时时间。
};
RestRequest请求=新的RestRequest(“/test/{FileName}”);
AddParameter(“文件名”,“testFile.abc”,ParameterType.UrlSegment);
字符串路径=@“C:/Users/[user]/Desktop/testFile.abc”;
var fileForDownload=client.DownloadData(请求);
fileForDownload.SaveAs(路径);
if(File.Exists(@“C:/Users/[user]/Desktop/testFile.abc”))
{
MessageBox.Show(“完成”);
}
progressBar1.值=100;
timer1.Stop();
}
public void timer1_Tick(对象发送方,事件参数e)
{
如果(progressBar1.Value=100)
{
progressBar1.值=0;
}
}

更改“timer1”的名称以命名最佳实践。祝你好运…

我认为更好的替代方法是重写FileStream以获得写入文件的字节数:

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);
}
其中HikFileStream是:

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!
    }

}

这个答案显示了如何计时一个恒定的10秒间隔。它实际上并没有给出任何关于下载了多少数据的指示。这对我来说不起作用。RestSharp似乎只在下载完成后执行ResponseWriter操作。