Ejemplo n.º 1
0
 public static Boolean CheckFormatoFile(HttpPostedFileBase file, Models.TipoMedia formato = Models.TipoMedia.FOTO)
 {
     if (file != null && file.ContentLength > 0)
     {
         string    estensione      = new FileInfo(Path.GetFileName(file.FileName)).Extension;
         string    nomeFileUnivoco = System.Guid.NewGuid().ToString() + estensione;
         FileMedia media           = Models.VerificaUpload.getIstanziaFile(estensione.ToLower());
         // formare stringa esadecimale
         string       hexFile   = string.Empty;
         Stream       stream    = file.InputStream;
         BinaryReader binary    = new BinaryReader(stream);
         byte[]       firmaFile = binary.ReadBytes(media.NumBytesID);
         foreach (byte B in firmaFile)
         {
             hexFile += B.ToString("X2");
         }
         if (media != null && media.checkFormato(hexFile) && media.TipoFile == formato)
         {
             //log.Debug("Tipo file verificato.");
             return(true);
         }
         else
         {
             // lanciare eccezione file non corrispondente all'estensione
             throw new InvalidDataException(App_GlobalResources.Language.ErrorFormatFile);
         }
     }
     return(false);
 }