Last active
February 24, 2019 20:03
-
-
Save scottsauber/6382aece63065679991a321d036c94b7 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
[Route("File")] | |
public class FileController : Controller | |
{ | |
private readonly IFileService _fileService; | |
public FileController(IFileService fileService) | |
{ | |
_fileService = fileService; | |
} | |
[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; | |
default: | |
throw new Exception($"Unknown extension {extension}"); | |
} | |
return File(file.Contents, contentType); | |
} | |
} | |
public class ApplicationFile | |
{ | |
public string NameWithExtension { get; set; } | |
public byte[] Contents { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment