Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[HttpPost("Download/{id:guid}")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Download(Guid id)
{
// Retrieve some file from some service
ApplicationFile file = await _fileService.GetFileAsync(id);
string extension = Path.GetExtension(file.NameWithExtension);
string contentType = "";
switch (extension)
{
case ".pdf":
contentType = "application/pdf";
break;
case ".xslx":
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case ".docx":
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
// and so on
default:
throw new ArgumentOutOfRangeException($"Unable to find Content Type for file name {file.NameWithExtension}.");
}
return File(file.Contents, contentType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment