1

I need some help. I need to fetch what the file type should be from the file containing only using Node.js. I am explaining the scenario below.

Let's 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."

As we can see here the variable fileContent has some text data and I want to write this data into one file. So for that, I need to create the file with a proper extension like abc.txt. Similarly, If fileContent has some json data then I will create the file like abc.json and write the value inside it.

So here I need to fetch what should be the type of file from this content only using Node.js. If anybody has a solution for this will be a great help.

2
  • If you want something more than just JSON and text it doesn't seem like there would be one liner solution. You need ML here or an existing product that gives you service to use their API. Commented Jan 28, 2022 at 6:07
  • Follow this question and answer stackoverflow.com/questions/10865347/node-js-get-file-extension Commented Jan 28, 2022 at 6:12

1 Answer 1

1

You can use popular file-type package for this. You can send buffer/file/stream/blob as an input and get file type as an output.

const FileType = require('file-type');
...
const { ext, mime } = await FileType.fromBuffer(your_data);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.