silverlight文件下载方法
2011-08-03 23:00
267 查看
silverlight来实现文件下载,纠结了很长的时间。一般的,如果是zip、rar等文件,直接通过NavigationService.Navigate(url);的形式即可,但是,如果是wav、MP3文件等,浏览器往往会直接打开应用程序进行播放(如Windows Media Player等),造成了很多的困惑。
网上搜索的解决方案,要么是通过silverlight客户端来下载,要么通过asp.net来下载,不一而足,各有优缺点。这里不评价各种优缺点,只记述了自己实现的结果。这里需要下载的文件是存放在服务器上的,并考虑到了虚拟目录的。一些动态生成的文件,也可以以此处理。
asp.net端的代码,短短的,没有几行:
silverlight端的处理:
网上搜索的解决方案,要么是通过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);
相关文章推荐
- silverlight 文件下载若干方法
- Silverlight文件上传下载实现方法(下载保存)
- 关于Struts2文件下载时,下载方法被重复执行的问题
- 下载的文件需要右键属性解除锁定的一次性解决方法
- Silverlight中用WCF实现文件快速上传(一、基本方法)
- 下载文件时文件名为乱码解决方法
- VB.NET中使用FTP下载文件的两种方法。
- 前端下载文件(GET、POST方法)
- C#文件下载方法 汇总
- PHP安全下载文件的方法
- JSP实现远程文件下载保存到服务器指定目录中的方法
- Linux远程下载文件的两种方法之 ftp命令和scp命令
- Chrome插件安装文件提取方法及 PwdHash Port 的插件下载
- FTPClient上传、下载文件方法
- ASP.NET中文件上传下载方法集合
- jsp下载文件的实现方法 及 注意
- cmd下载文件方法
- .net中下载文件的方法
- Rational Rose 2007 &Rational Rose 2003 下载及破解方法和汉化文件下载
- JavaEE_Web文件下载_lesson2_ JavaEE下载文件,中文乱码解决方法(Content-Disposition)