fork download
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. string ContentType = "application/octet-stream";
  10. string FileName = "test.xls";
  11. bool validate = false;
  12.  
  13. string[] AllowedTypes = { "application/vnd.ms-excel",
  14. "application/ms-excel",
  15. "application/x-msexcel",
  16. "application/excel",
  17. "application/x-excel",
  18. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  19. };
  20.  
  21. //Para o fallback, acaso o navegador enviar a extensão octet-stream ou zip
  22. string[] FallckExtensions = { ".xls", ".xlsx" };
  23. string[] FallbackMimes = { "application/octet-stream", "application/zip",
  24. "application/x-zip-compressed" };
  25.  
  26. //Se ContentType for um dos FallbackMimes, então checa a extensão
  27. if (FallbackMimes.Contains(ContentType)) {
  28. string extension = Path.GetExtension(FileName).ToLower();
  29.  
  30. validate = FallckExtensions.Contains(extension);
  31. } else {
  32. //Se o ContentType enviado pelo navegador não for octet-stream ou zip testa o AllowedTypes
  33. validate = AllowedTypes.Contains(ContentType);
  34. }
  35.  
  36. Console.Write(validate ? "Validou" : "Não validou");
  37. }
  38. }
  39.  
Success #stdin #stdout 0.02s 15568KB
stdin
Standard input is empty
stdout
Validou