我正在尝试将HttpPostedFileBase
的内容类型与可接受类型的数组进行比较,并且我遇到了一些真正奇怪的行为。
array.Contains()
找不到字符串,但如果我遍历集合并将每个条目与字符串进行比较,则会得到一个匹配项。
这是我的代码:
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!";
}
上面的代码输出以下内容
acceptable.Contains(type) = false
but application/octet-stream is definitely present in the string array!
如果我将file.ContentType
的打印值复制并粘贴到字符串变量中并运行比较,则可以正常工作。直接从HttpPostedFileBase.ContentType
读取值时,它才会失败,顺便提一下,System.String
类型为{{1}}。