Nodejs, to get file type of a file

不羁岁月 提交于 2020-01-04 03:18:18

问题


The six-year-old question "Node.js - File System get file type, solution around year 2012" has a better answer but the outdated answer was the correct one by then.

Hence the question, for an up-to-date solution.


回答1:


OPTION 1: If you want the mimetype:

Install

npm i -S file-type read-chunk

Use

const readChunk = require('read-chunk');
const fileType = require('file-type');

const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
console.log(fileType(buffer));

OPTION 2: If you just need the would-be file extension:

Install

npm i -S image-size

Use

const sizeOf = require('image-size');
console.log(sizeOf(filePath).type);


来源:https://stackoverflow.com/questions/53950321/nodejs-to-get-file-type-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!