C#HttpPostedFileBase.ContentType一个字符串比较失败,传递另一个字符串比较
我试图将C#HttpPostedFileBase.ContentType一个字符串比较失败,传递另一个字符串比较,c#,arrays,string-comparison,httppostedfilebase,C#,Arrays,String Comparison,Httppostedfilebase,我试图将HttpPostedFileBase的内容类型与可接受类型的数组进行比较,我遇到了一些真正奇怪的行为 array.Contains()找不到字符串,但是如果我迭代集合并将每个条目与字符串进行比较,它会得到一个匹配项 这是我的密码: string[] acceptable = new string[]{ "text/comma-separated-values", "text/csv", "text/anytext", "application/csv"
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!";
}
如果我将
file.ContentType
的打印值复制并粘贴到一个字符串变量中,并运行比较,则效果良好。它仅在直接从HttpPostedFileBase.ContentType
读取值时失败,顺便说一句,它的类型是System.String
如果我运行您的代码但替换string type=model.file.ContentType.ToLower().Trim()代码>使用string=“文本/逗号分隔值”。一切正常Ben我确实提到过。请看这篇文章的结尾。
acceptable.Contains(type) = false
but application/octet-stream is definitely present in the string array!