搜尋套件、儲存庫與 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 後端專案
  • 靜態檔案伺服器
  • 全-stack 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.

所屬分類

原始碼儲存庫

使用此套件的專案