首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

silverlight 下载文件能否弹出对话框。该如何处理

2012-04-25 
silverlight 下载文件能否弹出对话框。当我们从网上下载文件的时候,浏览器都会弹出一个 是否要下载的提示框

silverlight 下载文件能否弹出对话框。
当我们从网上下载文件的时候,浏览器都会弹出一个 是否要下载的提示框或则文件存放到什么地方 的提示,silverlight如何才能做到,如果可以,请给点例子

[解决办法]
#region 下载图片
private void btnDownload_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
//实例化button

//_file 要下载的文件路径
//string _file = "http://localhost:8080/EHRWCF/upload/2.rar"; //这里改成你网站里的文件路径
string _file = "http://localhost:53230/ClientBin/pics/a.zip";
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "rar";
sf.Filter = "rar压缩文件 (*.rar)|*.rar|zip压缩文件 (*.zip)|*.zip|所有文件 (*.*)|*.*";
sf.FilterIndex = 1;
if (sf.ShowDialog() != true) return;
Stream clientStream = sf.OpenFile();
Uri _uri = new Uri(_file, UriKind.Absolute);
WebClient client = new WebClient();

if (client.IsBusy)
{
client.CancelAsync();
}
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloadStream_DownloadProgressChanged);

client.OpenReadAsync(_uri, clientStream);

}

void clientDownloadStream_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//DownloadProgressChangedEventArgs.ProgressPercentage - 下载完成的百分比
//DownloadProgressChangedEventArgs.BytesReceived - 当前收到的字节数
//DownloadProgressChangedEventArgs.TotalBytesToReceive - 总共需要下载的字节数
//DownloadProgressChangedEventArgs.UserState - 用户标识

this.tbMsgString.Text = string.Format("完成百分比:{0} 当前收到的字节数:{1} 资料大小:{2} ",
e.ProgressPercentage.ToString() + "%",
e.BytesReceived.ToString(),
e.TotalBytesToReceive.ToString());

}
#region 异步文件下载
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//OpenReadCompletedEventArgs.Error - 该异步操作期间是否发生了错误
//OpenReadCompletedEventArgs.Cancelled - 该异步操作是否已被取消
//OpenReadCompletedEventArgs.Result - 下载后的 Stream 类型的数据
//OpenReadCompletedEventArgs.UserState - 用户标识
if (e.Error != null)
{
//txtinfo.Text += e.Error.Message;
MessageBox.Show(e.Error.ToString());
//MessageBox.Show("下载文件时出错,可能是文件已经不存在!");
return;
}
if (e.Cancelled != true)
{
Stream clientStream = e.UserState as Stream;
Stream serverStream = (Stream)e.Result;
byte[] buffer = new byte[serverStream.Length];
serverStream.Read(buffer, 0, buffer.Length);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Close();
serverStream.Close();
}
}
#endregion

热点排行