Created
February 24, 2019 20:04
-
-
Save scottsauber/8d89bd5e3f83cb7d974c209c1c254c6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost("Download/{id:guid}")] | |
[ValidateAntiForgeryToken] | |
public async Task<IActionResult> Download(Guid id) | |
{ | |
// Retrieve some file from some service | |
ApplicationFile file = await _fileService.GetFileAsync(id); | |
var fileProvider = new FileExtensionContentTypeProvider(); | |
// Figures out what the content type should be based on the file name. | |
if (!fileProvider.TryGetContentType(file.NameWithExtension, out string contentType)) | |
{ | |
throw new ArgumentOutOfRangeException($"Unable to find Content Type for file name {file.NameWithExtension}."); | |
} | |
// contentType will be application/pdf for .pdf's, application/vnd.openxmlformats-officedocument.presentationml.presentation for .pptx's, etc. | |
return File(file.Contents, contentType); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment