Hello Everyone,
I want to validate my file type from below list
doc,docx,xls,xlsx,ppt,pptx,pdf
if my given file name type exists in above list then only i need to process the file. else i need to ignore that file. how to check weather my file type exists in above list of file types
eg : my file name is : "D:\files\Get_Started_With_Smallpdf.pdf"
can you please help me on this
Thanks,
Krish
Madan ShekarPosted Aug 12, 2021, 7:09 AM
Here is the logic you can use as your requirement
var supportedTypes = new[] { "txt", "doc", "docx", "pdf", "xls", "xlsx" };
var fileExt = System.IO.Path.GetExtension(file.FileName).Substring(1);
Here, GetExtension method is used to get the extension of uploaded file that is file.FileName From the path of the system.
if (!supportedTypes.Contains(fileExt))
{
ErrorMessage = "File Extension Is InValid - Only Upload WORD/PDF/EXCEL/TXT File"; return ErrorMessage;
}