您的位置:首页 > 其它

silverlight使用webclient下载uri,并转化为stream

2015-10-01 16:08 302 查看

  private void Button_Click_1(object sender, RoutedEventArgs e)

{

// of the ASP.NET website.)
string uri = Application.Current.Host.Source.AbsoluteUri;
int index = uri.IndexOf("/ClientBin");
uri = uri.Substring(0, index) + "/ProductList.bin";
uri = uritxt.Text;
// Begin the download.
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
webClient.OpenReadAsync(new Uri(uri));
webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;

}

private void webClient_OpenReadCompleted(object sender,
OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
// (Add code to display error or degrade gracefully.)
}
else
{
Stream stream = e.Result;
BinaryReader reader = new BinaryReader(stream);
// (Now process the contents of the resource.)
reader.Close();
}
}

private void webClient_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
lblProgress.Text = e.ProgressPercentage.ToString() + " % downloaded.";
progressBar.Value = e.ProgressPercentage;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: