Below WebAPI code will validate input file format or extension of posted file. Current code snippet validate file format for .txt and .csv,
- public class CSVParserController : ApiController
- {
-
- public JsonResult Post()
- {
-
- for (int i = 0; i < System.Web.HttpContext.Current.Request.Files.Count; i++)
- {
- System.Web.HttpPostedFile hpf = System.Web.HttpContext.Current.Request.Files[i];
- if (hpf.ContentLength > 0)
- {
- if ((Path.GetExtension(hpf.FileName).ToLower() != ".txt") || (Path.GetExtension(hpf.FileName).ToLower() != ".csv") )
- {
- throw new ApplicationException("Error Occured-Invalid File Format. Please select only Txt and CSV format");
- }
- }
-
- }
- }
- }