Verify that the downloaded file contains only plain text

I have an ASP.NET MVC application that allows a user to upload a file that should only contain plain text.

I am looking for a simple approach to verify that a file really only contains text.

For my purposes, I am happy to define text as any of the characters that I can see on my GB QWERTY keyboard.

Business rules mean that my uploaded file will not contain any accented characters, so it does not matter if their code is accepted or rejected.

Approaches still haven't worked:

  • Content check; there is nothing good, as it depends on the file extension.
  • Check char.IsControl for each character; there is nothing good, as the file may contain pipe (|) characters, which are considered control characters.

I would prefer not to use the long Regex template to make it work.

+4
source share
1 answer

It looks like you want ASCII 32-126 characters plus several factors and ends, such as 9 (horizontal tab), carriage return and line feed, etc.

I would prefer not to use a long Regex to make this work.

As long as this doesn't mean “no regular expressions at all,” you can use the accepted answer from this stack overflow question (I added the horizontal tab character to the original):

 ^([^\x09\x0d\x0a\x20-\x7e\t]*)$ 
+2
source

Source: https://habr.com/ru/post/1302263/


All Articles