Last active
February 26, 2021 23:12
-
-
Save tocalai/df902e34af0716dce1f4d06021b1c9f7 to your computer and use it in GitHub Desktop.
Web api: FileController.
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
namespace FileAPI.Controllers | |
{ | |
//[ApiController] | |
[Route("api/[controller]")] | |
public class FileController : ControllerBase | |
{ | |
private readonly FileService _fileService; | |
public FileController(FileService fileService) | |
{ | |
_fileService = fileService; | |
} | |
// download file(s) to client according path: rootDirectory/subDirectory with single zip file | |
[HttpGet("Download/{subDirectory}")] | |
public IActionResult DownloadFiles(string subDirectory) | |
{ | |
try | |
{ | |
var (fileType, archiveData, archiveName) = _fileService.FetechFiles(subDirectory); | |
return File(archiveData, fileType, archiveName); | |
} | |
catch (Exception exception) | |
{ | |
return BadRequest($"Error: {exception.Message}"); | |
} | |
} | |
// upload file(s) to server that palce under path: rootDirectory/subDirectory | |
[HttpPost("upload")] | |
public IActionResult UploadFile([FromForm(Name = "files")] List<IFormFile> files, string subDirectory) | |
{ | |
try | |
{ | |
_fileService.SaveFile(files, subDirectory); | |
return Ok(new { files.Count, Size = FileService.SizeConverter(files.Sum(f => f.Length)) }); | |
} | |
catch (Exception exception) | |
{ | |
return BadRequest($"Error: {exception.Message}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment