This library adds an extension method to FileInfo & IFormFile to validate the file based on thier configuration(s).
- With package manager :
Install-Package FileValidator
- With dotnet cli :
dotnet add package FileValidator
- Other options and version history : https://www.nuget.org/packages/FileValidator
public async Task<IActionResult> OnPostUploadAsync(IFormFile file)
{
/*By default it validates all the files like Documents,Images, Videos, Audios*/
bool isValid = file.IsValidFile();
if(isValid){
var filePath = Path.GetTempFileName();
await file.CopyToAsync(File.OpenWrite(filePath));
return Ok(new { message = "File uploaded successfully." });
}
return BadRequest(new {message = "Invalid file"});
}
var fileInfo = new FileInfo("C:/mypath/document.pdf");
var isValid = fileInfo.IsValidFile();
//If you want to only validate the Images then you can pass the allowed FileType[] parameter.
var checkValidImage = fileInfo.IsValidFile(new FileType[] { FileType.Image })
var fileInfo = new FileInfo("C:/mypath/document.pdf");
//If you want to allow json documents then update the configuration
var fileValidatorConfig = new FileValidatorConfiguration();
fileValidatorConfig.DocumentFileExtensins.Add(".json");
fileValidatorConfig.DocumentMimeTypes.Add("application/json");
FileValidatorExtension.UpdateDefaultConfiguration(fileValidatorConfig);
var isValid = fileInfo.IsValidFile(); //Returns true
//If you want to remove any existing valid extension then you can remove the extension from the configuration
fileValidatorConfig.ImmageFileExtensions.Remove(".gif"); //This will return false on any gif file.
FileValidatorExtension.UpdateDefaultConfiguration(fileValidatorConfig);
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated. Feel free to request for any changes or any additional features.
Distributed under the MIT License. See LICENSE for more information.
Name: Muthukumar Thevar
Email: [email protected]
Project Link: https://github.com/mak-thevar/FileValidator