How to detect the file type if it's an excel file in JavaScript on the web?

Viewed 27

I am using the FileReader() API to read files on the browser, I want to render something on the screen conditionally based on the file the user uploads:

  • JSON.
  • CSV.
  • Excel.

How can I detect if the file uploaded is an excel file? Do you use libraries for that or is there a genius way we can detect that blindly without going through all the chances?

1 Answers

Running out of space in comments, so I'll add an answer. Files will usually have an extension, on both Windows and Linux, but if you don't want to rely on that you can just try parsing the file in each of the formats to find the one that succeeds.

Related