2
I am importing an excel file using Asp.net and validate to see if it is really an excel or not. I don’t want to use if
and else
, so I’m trying to do this through Annotations.
My main idea now is to use regular Expressions, found some on the internet to validate excel, tried all but always they return me the message saying that the file is not excel even when it is (All types of files that I post are being invalidated).
Does anyone know if it is really possible to do this validation on Asp.net with these reg. Expressions?
Follow the model code I’m trying to validate:
[RegularExpression(@"^ (?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(xls|xlsx)$
", ErrorMessage = "O arquivo selecionado precisa ser um excel")]
[Required]
[Display(Name = "Arquivo Excel")]
public HttpPostedFileBase ArquivoExcel { get; set; }
Hello @joaoigor welcome to Sopt. Take a look at this link right here from Sopt http://answall.com/questions/5115/comor- restricted-determinadas-extens%C3%B5es-de-files-and-save-in-database, I believe it’s related to what you’re looking for.
– Danilo Pádua
Please note that the fact that the file has the Excel extension does not mean that it is a valid Excel file. Similarly, you may have an Excel file with a different extension (example,
.xyz
) and remains a valid Excel file.– Omni