tags:

views:

184

answers:

5

When i use a file upload i use to check file contenttype with regular expressions... For ex

private bool IsImage(HttpPostedFile file)
    {
        if (file != null && Regex.IsMatch(file.ContentType, "image/\\S+") &&
          file.ContentLength > 0)
        {
            return true;
        }
        return false;
    }

This returns my file is an image or not... How to check it is a word(.doc/.docx) document or not using c#...

+4  A: 

DOC's mimetype is:

  • application/msword [official]
  • application/doc
  • appl/text
  • application/vnd.msword
  • application/vnd.ms-word
  • application/winword
  • application/word
  • application/x-msw6
  • application/x-msword

happy regex'ing

Edit:According to @Dan Diplo, you should also check for .docx's MIMEtypes

Axarydax
also fo docx - application/vnd.openxmlformats-officedocument.wordprocessingml.document
x2
All the Word 2007 mime-types can be found at http://technet.microsoft.com/en-us/library/ee309278.aspx They are quite a mouthful!
Dan Diplo
@Axarydax how to give for word file instead of `"image/\\S+"`
Pandiya Chendur
if you read there are many options. Why not just have a vector with these mimetypes in it and then check if the mimetype of the file exists in that list?
PoweRoy
@poweRoy i cant get you what you are trying to say... Can you post an answer..
Pandiya Chendur
please accept Axarydas answer, mine is just a clarification in code.
PoweRoy
A: 

Take a look at regexblid A library with regular expression.

Also good to know, regexbuddy can be used to validate and test regular expressions.

Rob
Bit useless to post regexblid when there are no regex for documents on that site.
PoweRoy
+1  A: 

if the content-type's are known (see Axarydax answer), why would you want to use regex?

Pawel Lesnikowski
+1  A: 

For example using Axarydax answer: (so no docx mime check)

List<String> wordMimeTypes = new List<String>();
wordMimeTypes.Add("application/msword");
wordMimeTypes.Add("application/doc");
wordMimeTypes.Add("appl/text");
wordMimeTypes.Add("application/vnd.msword");
wordMimeTypes.Add("application/vnd.ms-word");
wordMimeTypes.Add("application/winword");
wordMimeTypes.Add("application/word");
wordMimeTypes.Add("application/x-msw6");
wordMimeTypes.Add("application/x-msword");
//etc...etc...

if (wordMimeTypes.Contains(file.ContentType))
{
    //word document
}
else
{
    //not a word document
}

More readable than Regex because regex will become a pain in the ass when trying to make a expression for a dozen of mime types

PoweRoy
why is mine accepted as answer? Axarydax made the answer, I only provided some code to explain my comment to his answer.
PoweRoy
@PoweRoy: because the copy-paste-able anwser always gets the check mark.
Mef
A: 

Gumbo is right, it might just be easier to check the file's extension.

Path.GetExtension(file.FileName)
masenkablast