In this article I will explain with an example, how to get MIME Type of a File in .Net Core.
This article will illustrate how to determine the MIME Type of a File using its FileName (File Extension) in ASP.Net Core.
Note: For beginners in ASP.Net MVC Core, please refer my article ASP.Net MVC Core Hello World Tutorial with Sample Program example.
 
 
Folder (Directory) Location
The Folder (Directory) is located within the Project inside the wwwroot folder. Below is the screenshot of the Solution Explorer window where you can see the Folder (Directory) i.e. Files and it containing three files.
MIME Type .Net Core Example: Get MIME Type of a File in ASP.Net Core
 
 
Model
Following is a Model class named FileModel with one property i.e. FileName.
public class FileModel
{
    public string FileName { get; set; }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.StaticFiles;
 
 
Controller
The Controller consists of two Action methods.
Action method for handling File Display operation
Inside this Action method, the list of all files in the Files Folder (Directory) (present in the wwwroot Folder) is fetched using the IHostingEnvironment interface into a String Array using the GetFiles method of the Directory class.
Note: For more details about IHostingEnvironment interface, please refer Using IHostingEnvironment in ASP.Net Core.
 
Later, the values of the Array are copied to a Generic List collection consisting objects of FileModel class and then returned to the View.
 
Action method for handling File Download operation
This action method handles the File Download operation and when the ActionLink is clicked, the name of the File to be downloaded is sent to this method.
First, the physical (absolute) path of the File is generated by concatenating the WebRootPath (path of the wwwroot folder), the name of the Folder (which contains the files) and the File name.
Then File is downloaded by passing the physical (absolute) path and content type (MIME Type) to the object of PhysicalFileResult class.
public class HomeController : Controller
{
    private IHostingEnvironment Environment;
 
    public HomeController(IHostingEnvironment _environment)
    {
        Environment = _environment;
    }
 
    public IActionResult Index()
    {
        //Fetch all files in the Folder (Directory).
        string[] filePaths = Directory.GetFiles(Path.Combine(this.Environment.WebRootPath, "Files/"));
 
        //Copy File names to Model collection.
        List<FileModel> files = new List<FileModel>();
        foreach (string filePath in filePaths)
        {
            files.Add(new FileModel { FileName = Path.GetFileName(filePath) });
        }
 
        return View(files);
    }
 
    public PhysicalFileResult DownloadFile(string fileName)
    {
        //Determine the Content Type of the File.
        string contentType = "";
        new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
 
        //Build the File Path.
        string path = Path.Combine(this.Environment.WebRootPath, "Files/") + fileName;
 
 
        //Send the File to Download.
        return new PhysicalFileResult(path, contentType);
    }
}
 
 
View
Inside the View, the FileModel class is declared as List which specifies that it will be available as a Collection.
Displaying the Files
For displaying the files, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the File records.
 
Downloading the File
The HTML Table contains an ActionLink for downloading the File.
When the Download link is clicked, the ActionLink calls the DownloadFile Action method and the File is downloaded.
@using MVC_Core_PhysicalFileResult.Models
@model List<FileModel>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <th>File Name</th>
            <th></th>
        </tr>
        @foreach (FileModel file in Model)
        {
            <tr>
                <td>@file.FileName</td>
                <td>@Html.ActionLink("Download", "DownloadFile", new { fileName = file.FileName })</td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Screenshot
MIME Type .Net Core Example: Get MIME Type of a File in ASP.Net Core
 
 
Downloads