Skip to content

Instantly share code, notes, and snippets.

@f7q
Created May 30, 2018 13:07
Show Gist options
  • Save f7q/1719d1ca58d1231a9b4a7c81c9688f77 to your computer and use it in GitHub Desktop.
Save f7q/1719d1ca58d1231a9b4a7c81c9688f77 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC Fileダウンロード

HomeController.cs

public IActionResult Download()
{
  var mimeType = "text/xml";
  var xml = @"
<xml>
<var>テスト</var>
</xml>";
  var fileContent = System.Text.Encoding.UTF8.GetBytes(xml);
  var stream = new MemoryStream(fileContent);
  var fileStreamResult = new FileStreamResult(stream, mimeType);
  fileStreamResult.FileDownloadName = "FileStreamExample.xml";
  return fileStreamResult;
}

Index.chtml

<a asp-action="Download" asp-controller="Home">Download</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment