6.68FileTypeMapper File MIME Type Mapping

Created on Aug 17, 2026~3 min read

During file upload and download, it is often necessary to obtain or set the MIME type based on the file extension. This step usually requires developers to configure it manually and correctly. To reduce the developer's burden, the framework provides the FileTypeMapper type, which can automatically return the corresponding MIME type based on the file extension. For example:

cs
var fileTypeMapper = new FileTypeMapper();fileTypeMapper.TryGetContentType("image.jpg", out var mimeType); // mimeType is "image/jpeg"fileTypeMapper.TryGetContentType("image.png", out mimeType);    // mimeType is "image/png"

The framework has a built-in dictionary containing 389 file extensions and their corresponding MIME types, covering almost all mainstream and non-mainstream file types.

In addition, all methods provided by the framework for adding file content (such as AddFile, AddFileAsStream, AddFileAsByteArray, AddFileFromRemote, etc.) automatically resolve the file's MIME type when the user does not specify the contentType parameter. If it cannot be resolved, it defaults to application/octet-stream. For example:

cs
var content = await httpRemoteService.PostAsStringAsync("https://localhost:7044/HttpRemote/AddForm?id=1",    builder => builder.SetMultipartContent(multipart =>    {        // Without passing contentType, it is automatically resolved to "image/jpeg"        multipart.AddFileAsStream(@"C:\Workspaces\httptest.jpg", "file")    }));

With the framework's automatic resolution, developers no longer need to manually look up the file's MIME type when uploading files, greatly simplifying the workflow.