In the code below, it would work perfectly if I had access to the path
of the uploaded file on the client side, however, it does not work that way. So I'd like to ask your suggestion for another way to do this.
Basically I need a column counter, however my file is in memory, since it is a HttpPostedFileBase
.
string result = new StreamReader(model.Arquivo.InputStream).ReadToEnd();
var lines = System.IO.File.ReadAllLines(@"caminhoQualquer\arquivorandom.txt", Encoding.UTF7);
for (int i = 0; i < lines.Length; i++)
{
var columns = lines[i].Split(';').Count();
if(columns > 9)
throw new Exception("Não foi possível importar, pois o arquivo não tem a quantidade de colunas esperadas.");
}
Thank you for all the answers.
Note: On the path question, I opened this topic yesterday: How to get the path of a file (HttpPostedFileBase)?
But I did not succeed.
Edit:
How the entry is:
Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field Field; field; field; campfield; field
In this case, the system would have to validate row by row the number of columns, if by chance one row has more columns than the parameterized amount, will raise the error.