Find File Type From Magic Number Of File In MVC 5

Introduction

In this article we are going to upload a document from html input & we can find the magic number of that file & from that number we can find the type of that file.

Magic Number:

  • A magic number is a number embedded at or near the beginning of a file that indicates its file format (i.e. the type of file it is).
  • This number is not visible to us.
  • Every file has a number which represents name of file types which is hexadecimal format.
  • In our program we will convert it into bytes and array for checking file type from.

Chart for magic number

table

Note: Refer chart source for file types Wikipedia.

Step 1: Create simple project in Visual Studio 2013.

Empty template

Step 2: Create one controller (right click on controller folder, add, then controller).

Controller

Step 3: Now from Index action create single view (right click on action method => add view) for this action.

Add view

Step 4: Now add code for UI which will represent one input file button, one submit button & some label (to show error, success, file name, etc).

  1. <h2>Find File Type From Magic Number of File</h2>  
  2. @using (Html.BeginForm("Index",  
  3. "File_Upload",  
  4. FormMethod.Post,  
  5. new { enctype = "multipart/form-data" }))  
  6. {  
  7.    <label for="file">Upload file :</label>  
  8.    <input type="file" name="file" id="file" /><br>  
  9.    <input type="submit" id="submit" /> <br />  
  10.    <p>Program_Errors will show here:</p>  
  11.    @ViewBag.error  
  12.    <p>You have uploaded file type is : @ViewBag.File_Type </p>  
  13.    <br />  
  14.    @ViewBag.Message  
  15.    <br />  
  16.    @ViewBag.signature  
  17.    <br />  
  18.    @ViewBag.out_put  
  19. }  
Step 5: Now create another action method with same name as index & pass ttpPostedFileBase  object.

In our action we will do the following things:

 

  • Will create one array in which we can store above magic number so that it will help us at the end to compare file type in switch case.

  • We need to add namespace using System.IO;

  • Create one folder in our solution file as “Docs” to upload a document in this folder (download code for more information).

  • Substring to select first 11 characters from hexadecimal array because every file has a different magic number so I chose only three which have same characters.
    1. [HttpPost]  
    2.   
    3. public ActionResult Index(HttpPostedFileBase file)  
    4. {  
    5.   
    6.     string ext = null;  
    7.   
    8.     string[] file_hexa_signature = {  
    9.         "38-42-50-53""25-50-44-46""4D-4D-00-2A""4D-4D-00-2A"  
    10.     };  
    11.   
    12.     try  
    13.   
    14.     {  
    15.   
    16.         ext = System.IO.Path.GetExtension(file.FileName).ToLower();  
    17.   
    18.     } catch (Exception ex)  
    19.   
    20.     {  
    21.   
    22.         ViewBag.error = ex.Message;  
    23.   
    24.     }  
    25.   
    26.     if (ext == null)  
    27.   
    28.     {  
    29.   
    30.         ViewBag.error = "please select onlt psd, pdf, tif file types only";  
    31.   
    32.     }  
    33.   
    34.   
    35.     if (file != null && file.ContentLength > 0)  
    36.   
    37.     {  
    38.   
    39.         string str = Path.GetFileName(file.FileName);  
    40.   
    41.         string path = Path.Combine(Server.MapPath("~/Docs"),  
    42.   
    43.             Path.GetFileName(file.FileName));  
    44.   
    45.         file.SaveAs(path);  
    46.   
    47.         BinaryReader reader = new BinaryReader(new FileStream(Convert.ToString(path), FileMode.Open, FileAccess.Read, FileShare.None));  
    48.   
    49.         reader.BaseStream.Position = 0x0; // The offset you are reading the data from  
    50.   
    51.         byte[] data = reader.ReadBytes(0x10); // Read 16 bytes into an array  
    52.   
    53.         string data_as_hex = BitConverter.ToString(data);  
    54.   
    55.         reader.Close();  
    56.   
    57.         // substring to select first 11 characters from hexadecimal array  
    58.   
    59.         string my = data_as_hex.Substring(0, 11);  
    60.   
    61.         string output = null;  
    62.   
    63.         switch (my)  
    64.   
    65.         {  
    66.   
    67.             case "38-42-50-53":  
    68.   
    69.                 output = " => psd";  
    70.   
    71.                 break;  
    72.   
    73.             case "25-50-44-46":  
    74.   
    75.                 output = " => pdf";  
    76.   
    77.                 break;  
    78.   
    79.             case "49-49-2A-00":  
    80.   
    81.                 output = " => tif";  
    82.   
    83.                 break;  
    84.   
    85.             case "4D-4D-00-2A":  
    86.   
    87.                 output = " => tif";  
    88.   
    89.                 break;  
    90.   
    91.             case "null":  
    92.   
    93.                 output = "file type is not matches with array";  
    94.   
    95.                 break;  
    96.   
    97.         }  
    98.   
    99.         ViewBag.Message = data_as_hex;  
    100.   
    101.         ViewBag.signature = my;  
    102.   
    103.         ViewBag.out_put = output;  
    104.   
    105.     }  
    106.   
    107.     return View();  
    108.   
    109. }  

Step 6: Now run our program.

run our program

Step 7: Now click on browse button & select file as shown.

click on browse button

Step 8: After selecting file it will show the file name.

shows file name

Step 9: Now click on submit query & our output will be like. the following

click on submit query

Summary

This article will help freshers as well as experience candidates.

Hope you enjoyed this complete article. Don’t forget to comment.

Up Next
    Ebook Download
    View all
    Learn
    View all