代码之家  ›  专栏  ›  技术社区  ›  xpt

Nodejs,获取文件的文件类型

  •  1
  • xpt  · 技术社区  · 7 年前

    “六年问题” Node.js - File System get file type, solution around year 2012 “有一个更好的答案,但过时的答案是正确的。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Stephen Paul    7 年前

    选项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);
    
    推荐文章