Silverlightファイルのダウンロード方法


silverlightはファイルのダウンロードを実現するために、長い間悩んでいました.一般的にzip、rarなどのファイルであれば、NavigationServicesを直接通過する.Navigate(url);ただし、wav、MP 3ファイルなどであれば、ブラウザが直接アプリケーションを開いて再生(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);