【发布时间】:2018-12-28 13:55:07
【问题描述】:
六年前的问题“Node.js - File System get file type, solution around year 2012”有更好的答案,但过时的答案是正确的。
因此提出了一个问题,以获得最新的解决方案。
【问题讨论】:
标签: javascript node.js filesystems file-type
六年前的问题“Node.js - File System get file type, solution around year 2012”有更好的答案,但过时的答案是正确的。
因此提出了一个问题,以获得最新的解决方案。
【问题讨论】:
标签: javascript node.js filesystems file-type
选项 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);
【讨论】:
mime":"application/x-msdownload",但它为二进制文件返回undefined,file 命令正确返回ELF 64-bit LSB shared object, x86-64...