Nodejs,获取文件的文件类型
Nodejs, to get file type of a file
六年前的问题“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);
六年前的问题“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);