Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check file extension using React

I want to check whether the file extension is an image or a pdf file. But using my code, only else part is working.

 const thumbs = files.map((selectedFile) => (
    <div key={selectedFile["name"]} className="d-flex">
      {selectedFile['.type'] == ".pdf" ? (
        <iframe src={selectedFile["preview"]} style={{ height: "20%", width: "20%" }} />
      ) : (
        <img
          src={selectedFile["preview"]}
          alt={selectedFile["name"]}
          style={{ height: "20%", width: "20%" }}
        />
      )}
    </div>
  ));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You would implement a function to check that. Example:

function getExtension(filename) {
  return filename.split('.').pop()
}

And then in the code:

getExtension(selectedFile["name"]).toLowerCase() === "pdf" ? ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading