0

I'm trying to compare the content type of an HttpPostedFileBase against an array of acceptable types, and I'm encountering some genuinely odd behaviour.

array.Contains() can't find the string, but if I iterate over the collection and compare each entry to the string, it gets a match.

Here's my code:

string[] acceptable = new string[]{ 
    "text/comma-separated-values", 
    "text/csv",
    "text/anytext",
    "application/csv",
    "application/excel",
    "application/octet-stream",
    "application/vnd.ms-excel",
    "application/vnd.msexcel"
};

string type = model.file.ContentType.ToLower().Trim();

string err = "acceptable.Contains(type) = " + 
             (acceptable.Contains(type) ? "true" : "false");

err += "<br/><br/>";

foreach (string s in acceptable)
{
    if (s == type)
        err += "but " + type + " is definitely in the string array!"; 
}

The code above outputs the following

acceptable.Contains(type) = false    

but application/octet-stream is definitely present in the string array!

If I copy and paste the printed value of file.ContentType into a string variable and run the comparison, it works fine. It only fails when reading the value directly from HttpPostedFileBase.ContentType, which is of type System.String by the way.

roryok
  • 9,325
  • 17
  • 71
  • 138
  • IF I run your code but replace `string type = model.file.ContentType.ToLower().Trim();` with string = "text/comma-separated-values". Everything works as expected. – Ben Robinson Oct 09 '13 at 09:20
  • Ben, I did mention that. See the end of the post. – roryok Oct 09 '13 at 09:21

0 Answers0