get-file-information
v2.0.0
Published
Get comprehensive file info in both browser and Node.js — size, MD5/SHA256, dimensions, MIME type, and more
Maintainers
Readme
get-file-information
🌐 同时在浏览器和 Node.js 中获取全面的文件信息 / Get comprehensive file info in both browser and Node.js
中文
安装
npm install get-file-information快速开始
浏览器
import { getFileInfo } from 'get-file-information'
const input = document.querySelector('input[type="file"]')
input.addEventListener('change', async (e) => {
const file = e.target.files[0]
const info = await getFileInfo(file)
console.log(info)
// {
// name: "photo.jpg",
// stem: "photo",
// extension: ".jpg",
// size: 2048576,
// sizeFormatted: "1.95 MB",
// mimeType: "image/jpeg",
// md5: "d41d8cd98f00b204e9800998ecf8427e",
// sha256: "e3b0c442...",
// lastModifiedFormatted: "2024-11-15 09:30:00",
// width: 1920,
// height: 1080
// }
})Node.js
import { getFileInfo } from 'get-file-information'
const info = await getFileInfo('/path/to/photo.jpg')
console.log(info)
// 浏览器字段 + Node.js 专属:
// { absolutePath, birthtime, atime, mode, isDirectory, isSymlink, ino }工具函数
import { formatSize, formatTime, getExtension, getStem, getBaseName, inferMimeType } from 'get-file-information'
formatSize(2048576) // "1.95 MB"
formatTime(1700000000000) // "2024-11-15 09:30:00"
getExtension('photo.jpg') // ".jpg"
getStem('photo.jpg') // "photo"
getBaseName('/a/b/c.txt') // "c.txt"
inferMimeType('data.json') // "application/json"API
getFileInfo(input: File | string) → Promise<FileInfo | NodeFileInfo>
通用字段
| 字段 | 类型 | 说明 |
|---|---|---|
| name | string | 文件名 |
| stem | string | 不含后缀的文件名 |
| extension | string | 后缀,如 ".jpg" |
| size | number | 文件大小(字节) |
| sizeFormatted | string | 可读大小,如 "2.35 MB" |
| mimeType | string | MIME 类型,如 "image/jpeg" |
| md5 | string | MD5 哈希 |
| sha256 | string | SHA-256 哈希 |
| lastModified | number | 最后修改时间戳(ms) |
| lastModifiedFormatted | string | 格式化时间 |
| width? | number | 图片/视频宽度 |
| height? | number | 图片/视频高度 |
| duration? | number | 视频时长(秒,仅浏览器) |
Node.js 专属字段
| 字段 | 类型 | 说明 |
|---|---|---|
| absolutePath | string | 绝对路径 |
| birthtime | number | 创建时间戳 |
| birthtimeFormatted | string | 格式化创建时间 |
| atime | number | 最后访问时间戳 |
| atimeFormatted | string | 格式化访问时间 |
| mode | number | 文件权限 |
| isDirectory | boolean | 是否目录 |
| isSymlink | boolean | 是否符号链接 |
| ino | number | inode 编号 |
兼容性
| 浏览器 | MD5 | SHA-256 | 媒体尺寸 | |---|---|---|---| | Chrome 37+ | ✅ | ✅ | ✅ | | Firefox 34+ | ✅ | ✅ | ✅ | | Safari 11+ | ✅ | ✅ | ✅ | | Edge 79+ | ✅ | ✅ | ✅ |
Node.js 16+
English
Install
npm install get-file-informationQuick Start
Browser
import { getFileInfo } from 'get-file-information'
const input = document.querySelector('input[type="file"]')
input.addEventListener('change', async (e) => {
const info = await getFileInfo(e.target.files[0])
})Node.js
import { getFileInfo } from 'get-file-information'
const info = await getFileInfo('/path/to/file.jpg')Utilities
import {
formatSize, formatTime, getExtension,
getStem, getBaseName, inferMimeType
} from 'get-file-information'API
getFileInfo(input: File | string) → Promise<FileInfo | NodeFileInfo>
- Browser: pass a
Fileobject → returnsFileInfo - Node.js: pass a path string → returns
NodeFileInfo(withabsolutePath,birthtime,atime, etc.)
See the Chinese section above for the full field table.
Browser Support
| Browser | Md5 | SHA-256 | Dimensions | |---|---|---|---| | Chrome 37+ | ✅ | ✅ | ✅ | | Firefox 34+ | ✅ | ✅ | ✅ | | Safari 11+ | ✅ | ✅ | ✅ | | Edge 79+ | ✅ | ✅ | ✅ |
Node.js 16+
🚀 Live Demo
👉 https://wlxweb.github.io/getFileInfo/
Drag & drop any file to see its full metadata — size, hashes, dimensions, MIME type, and more.
License
ISC © wlxweb
