“六年问题” Node.js - File System get file type, solution around year 2012 “有一个更好的答案,但过时的答案是正确的。
选项1:如果需要mimetype :
安装
npm i -S file-type read-chunk
使用
const readChunk = require('read-chunk'); const fileType = require('file-type'); const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes); console.log(fileType(buffer));
选项2:如果您只需要可能的文件扩展名
npm i -S image-size
const sizeOf = require('image-size'); console.log(sizeOf(filePath).type);