ASP.NET MVC ファイルのダウンロード
ASP.NET MVCでファイルをダウンロードする方法メモ
Controller サーバーのフォルダにファイルが存在する場合
public ActionResult Download() { var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return File(Server.MapPath("~/App_Data/dummy.xlsx"), contentType, "download.xlsx"); }
FileStreamからダウンロード
public ActionResult Download() { MemoryStream memoryStream = new MemoryStream(); using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) { fileStream.CopyTo(memoryStream); } var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; return File(memoryStream.ToArray(), contentType, "download.xlsx"); }