using System;
using System.IO;
using System.Linq;
public class Test
{
public static void Main()
{
string ContentType = "application/octet-stream";
string FileName = "test.xls";
bool validate = false;
string[] AllowedTypes = { "application/vnd.ms-excel",
"application/ms-excel",
"application/x-msexcel",
"application/excel",
"application/x-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
};
//Para o fallback, acaso o navegador enviar a extensão octet-stream ou zip
string[] FallckExtensions = { ".xls", ".xlsx" };
string[] FallbackMimes = { "application/octet-stream", "application/zip",
"application/x-zip-compressed" };
//Se ContentType for um dos FallbackMimes, então checa a extensão
if (FallbackMimes.Contains(ContentType)) {
string extension = Path.GetExtension(FileName).ToLower();
validate = FallckExtensions.Contains(extension);
} else {
//Se o ContentType enviado pelo navegador não for octet-stream ou zip testa o AllowedTypes
validate = AllowedTypes.Contains(ContentType);
}
Console.Write(validate ? "Validou" : "Não validou");
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uSU87CnVzaW5nIFN5c3RlbS5MaW5xOwoKcHVibGljIGNsYXNzIFRlc3QKewogICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIHN0cmluZyBDb250ZW50VHlwZSA9ICJhcHBsaWNhdGlvbi9vY3RldC1zdHJlYW0iOwogICAgICAgIHN0cmluZyBGaWxlTmFtZSA9ICJ0ZXN0LnhscyI7CiAgICAgICAgYm9vbCB2YWxpZGF0ZSA9IGZhbHNlOwoKICAgICAgICBzdHJpbmdbXSBBbGxvd2VkVHlwZXMgPSB7ICJhcHBsaWNhdGlvbi92bmQubXMtZXhjZWwiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgImFwcGxpY2F0aW9uL21zLWV4Y2VsIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJhcHBsaWNhdGlvbi94LW1zZXhjZWwiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgImFwcGxpY2F0aW9uL2V4Y2VsIiwKICAgICAgICAgICAgICA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