LoginSignup
12
13

More than 5 years have passed since last update.

WebAPI でファイルダウンロード

Last updated at Posted at 2014-05-07

いつも探すのでメモ。
もっとスマートなやり方がある気がしてならない。

return ResponseMessage(new HttpResponseMessage
{
    Content = new StreamContent(File.Open(filePath, FileMode.Open, FileAccess.Read))
    {
        Headers =
        {
            ContentType = new MediaTypeHeaderValue("application/octet-stream"),
            ContentDisposition = new ContentDispositionHeaderValue("attatchment")
            {
                FileName = fileName
            }
        },
    }
});

条件によって他の IHttpActionResult を返したくなった場合に生の HttpResponseMessage を返していると困るので、ReponseMessage メソッドで包んで返す。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13