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

Chart for the magic number

Magic table

Note. Refer to chart source for file types Wikipedia.

Step 1. Create a simple project in Visual Studio 2013.

Empty template

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

Controller

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

Index action

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).

<h2>Find File Type From Magic Number of File</h2>
@using (Html.BeginForm("Index", "File_Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <label for="file">Upload file :</label>
    <input type="file" name="file" id="file" /><br>
    <input type="submit" id="submit" /><br />
    <p>Program_Errors will show here:</p>
    @ViewBag.error
    <p>You have uploaded file type is: @ViewBag.File_Type </p>
    <br />
    @ViewBag.Message
    <br />
    @ViewBag.signature
    <br />
    @ViewBag.out_put
}

Step 5. Now create another action method with the same name as the index & pass the ttpPostedFileBase object.

In our action, we will do the following things.

[HttpPost]   

public ActionResult Index(HttpPostedFileBase file)   
{   
    string ext = null;   
    string[] file_hexa_signature = {   
        "38-42-50-53",   
        "25-50-44-46",   
        "4D-4D-00-2A",   
        "4D-4D-00-2A"   
    };   
   
    try   
    {   
        ext = System.IO.Path.GetExtension(file.FileName).ToLower();   
    }   
    catch (Exception ex)   
    {   
        ViewBag.error = ex.Message;   
    }   
   
    if (ext == null)   
    {   
        ViewBag.error = "please select onlt psd, pdf, tif file types only";   
    }   
   
    if (file != null && file.ContentLength > 0)   
    {   
        string str = Path.GetFileName(file.FileName);   
        string path = Path.Combine(Server.MapPath("~/Docs"),   
            Path.GetFileName(file.FileName));   
        file.SaveAs(path);   
        BinaryReader reader = new BinaryReader(new FileStream(Convert.ToString(path), FileMode.Open, FileAccess.Read, FileShare.None));   
        reader.BaseStream.Position = 0x0; // The offset you are reading the data from   
        byte[] data = reader.ReadBytes(0x10); // Read 16 bytes into an array   
        string data_as_hex = BitConverter.ToString(data);   
        reader.Close();   
   
        // substring to select first 11 characters from hexadecimal array   
        string my = data_as_hex.Substring(0, 11);   
        string output = null;   
        switch (my)   
        {   
            case "38-42-50-53":   
                output = " => psd";   
                break;   
   
            case "25-50-44-46":   
                output = " => pdf";   
                break;   
   
            case "49-49-2A-00":   
                output = " => tif";   
                break;   
   
            case "4D-4D-00-2A":   
                output = " => tif";   
                break;   
   
            case "null":   
                output = "file type is not matches with array";   
                break;   
   
        }   
   
        ViewBag.Message = data_as_hex;   
        ViewBag.signature = my;   
        ViewBag.out_put = output;   
    }   
    return View();   
}

Step 6. Now run our program.

Run our program

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

Click on browse button

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

Shows file name

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

Submit query

Summary

This article will help freshers as well as experienced candidates.

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