您的位置:首页 > 其它

silverlight文件下载方法

2011-08-03 23:00 267 查看
silverlight来实现文件下载,纠结了很长的时间。一般的,如果是zip、rar等文件,直接通过NavigationService.Navigate(url);的形式即可,但是,如果是wav、MP3文件等,浏览器往往会直接打开应用程序进行播放(如Windows Media Player等),造成了很多的困惑。
网上搜索的解决方案,要么是通过silverlight客户端来下载,要么通过asp.net来下载,不一而足,各有优缺点。这里不评价各种优缺点,只记述了自己实现的结果。这里需要下载的文件是存放在服务器上的,并考虑到了虚拟目录的。一些动态生成的文件,也可以以此处理。
asp.net端的代码,短短的,没有几行:

public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString["filename"];
string physical_file_name = context.Server.MapPath(filename);

FileInfo fi = new FileInfo(physical_file_name);
//context.Response.Output.WriteLine("物理文件名:" + physical_file_name);

context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment;  filename=" + fi.Name);
context.Response.WriteFile(physical_file_name);
}


silverlight端的处理:

NavigationService.Navigate(url);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: