「FileStreamResult クラス (System.Web.Mvc)」の版間の差分
imported>Administrator |
imported>Administrator |
||
| 7行目: | 7行目: | ||
ストリーム上のCSVファイルを送信し、ダウンロードの確認ダイアログを表示する例。 | ストリーム上のCSVファイルを送信し、ダウンロードの確認ダイアログを表示する例。 | ||
<source lang="csharp"> | <source lang="csharp"> | ||
using System.Web.Mvc; | |||
using System.IO; | |||
// Streamを取得する。 | public class HomeController : Controller | ||
{ | |||
public FileStreamResult DownloadCsv() | |||
{ | |||
// Content-Dispositionヘッダーに「attachement」と設定することで、 | |||
// ブラウザにダウンロードするかの確認ダイアログを表示させる。 | |||
} | // | ||
// Content-Dispositionの値 | |||
// attachment = ダウンロード | |||
// inline = インライン表示 | |||
// | |||
// 「filename=」という部分を書き換えれば、多くのブラウザで保存 | |||
// ダイアログの初期ファイル名となる。IE5.5以前はダメらしい。 | |||
// さすがにもう存在しないだろう。 | |||
this.HttpContext.Response.AddHeader( | |||
"Content-Disposition", | |||
"attachment; filename=file.csv"); | |||
// Streamを取得する。 | |||
// CreateCsvメソッドは仮にあるものとする。 | |||
Stream fileStream = CreateCsv(); | |||
// fileStreamの内容を結果として送信する。 | |||
return new FileStreamResult(fileStream, "text/csv"); | |||
} | |||
} | |||
</source> | </source> | ||