0

我正在尝试将 an 的内容类型与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

4

0 回答 0