简体   繁体   English

如何使用 Node.js 从文件内容中获取文件扩展名

[英]How to get file extension from file content using Node.js

I need one help.我需要一个帮助。 I need to fetch what the file type should be from the file content only using Node.js.我需要仅使用 Node.js 从文件内容中获取文件类型。 I am exaplining the scenario below.我正在解释下面的场景。

Lets say I have some content like below.可以说我有一些如下内容。

let fileContent = "The textContent property sets or returns the text content of the specified node, and all its descendants."

If we can see here the variable fileContent has some text data and I want to write this data into one file.如果我们可以在这里看到变量fileContent有一些文本数据,我想将这些数据写入一个文件。 So for that I need to create the file with proper extension like abc.txt .因此,为此我需要创建具有适当扩展名的文件,例如abc.txt Similarly If fileContent has some json data then I will create the file like abc.json and write the value inside it.同样,如果fileContent有一些json数据,那么我将创建abc.json之类的文件并将值写入其中。

So here I need to fetch what should be the type of file from this content only using Node.js .因此,在这里我需要仅使用Node.js从此内容中获取文件的type If anybody has solution for this will be great help.如果有人对此有解决方案,将有很大帮助。

You can use popular file-type package for this.为此,您可以使用流行的文件类型package。 You can send buffer/file/stream/blob as an input and get file type as an output.您可以发送缓冲区/文件/流/blob 作为输入并获取文件类型为 output。

const FileType = require('file-type');
...
const { ext, mime } = await FileType.fromBuffer(your_data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM