Search packages, repositories and app categories

Package detailsv1.0.1

mime_type

MIME type library for Dart HTTP server applications.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
656K
Likes
73
Pub points
135

Install package

flutter pub add mime_type

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A lightweight MIME type library for Dart, designed to support HTTP server applications by enabling accurate detection and handling of file types based on extensions or content. It provides a simple API to map file extensions to their corresponding MIME types, ensuring proper content negotiation in web services. The package is optimized for use across multiple platforms, including mobile, web, and desktop environments. It integrates seamlessly with Dart's HTTP server frameworks and enhances compatibility with clients expecting correct Content-Type headers.

Use cases

  • HTTP server content type management
  • File upload handling
  • API response header configuration
  • Static file serving
  • Web application middleware
  • Content negotiation

Key features

  • Extension-to-MIME mapping
  • Cross-platform compatibility
  • Lightweight and fast
  • Easy integration with HTTP servers
  • Supports common and custom MIME types
  • Minimal dependencies

Suitable for

  • Dart HTTP server developers
  • Web API backend projects
  • Static file servers
  • Full-stack Dart applications
  • Microservices with file handling

Considerations

  • Limited to MIME type lookup only
  • No file validation or parsing
  • Requires manual extension handling
  • Not suitable for client-side MIME detection
  • Does not support automatic content sniffing

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

MIME type

Library to get MIME type from a file name or a file path and get the file extension from a MIME type. When a HTTP server sends a file to the client, MIME type of the file must be set to the Content-Type header of the response.

Only three methods are available:

  • String mime(String FileName) // gets the MIME type from a file name (such as 'Hello.dart') or a file path (such as '..\Hello.dart').

  • String mimeFromExtension(String extension) // gets the MIME type from an extension (such as 'dart')

  • String extensionFromMime(String mime) // gets extension from MIME type (returns null if there is no such mime type)

All methods return null if they cannot find a match.

Example

import 'package:mime_type/mime_type.dart';

sendFile(HttpRequest request, String fileName) {
  HttpResponse response = request.response;
  File file = new File(fileName);
  if (file.existsSync()) {
    String mimeType = mime(fileName);
    if (mimeType == null) mimeType = 'text/plain; charset=UTF-8';
      // you can change the default content type
      // or, you can choose to send error message
    response.headers.set('Content-Type', mimeType);
    RandomAccessFile openedFile = file.openSync();
    response.contentLength = openedFile.lengthSync();
    openedFile.closeSync();
    file.openRead().pipe(response);
  } else {
    // send 404 (Not Found) status to the client
  }
}

License

This library is licensed under MIT License.

Categories

Source repository

Projects using this package