搜索插件、代码仓与 APP 分类

插件详情v1.0.1

mime_type

用于 Dart HTTP 服务器应用程序的 MIME 类型库。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
65.6万
喜欢数
73
Pub 分数
135

安装插件

flutter pub add mime_type

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的Dart MIME类型库,专为HTTP服务器应用设计,通过文件扩展名或内容准确检测和处理文件类型。它提供了一个简单的API,用于将文件扩展名映射到相应的MIME类型,确保Web服务中的正确内容协商。该包经过优化,可在多个平台(包括移动、Web和桌面环境)上使用。它与Dart的HTTP服务器框架无缝集成,并增强与期望正确Content-Type头的客户端的兼容性。

适合场景

  • HTTP服务器内容类型管理
  • 文件上传处理
  • API 响应头配置
  • 静态文件服务
  • Web 应用中间件
  • 内容协商

主要特点

  • 扩展名到MIME类型的映射
  • 跨平台兼容性
  • 轻便且快速
  • 与 HTTP 服务器的轻松集成
  • 支持常见和自定义 MIME 类型
  • 最小依赖

适合对象

  • Dart HTTP服务器开发者
  • Web API 后端项目
  • 静态文件服务器
  • 全栈 Dart 应用
  • 微服务与文件处理

使用建议

  • 仅限 MIME 类型查找
  • 无文件验证或解析
  • 需要手动处理扩展
  • 不适用于客户端 MIME 检测
  • 不支持自动内容嗅探

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

源码仓库

使用此插件的项目