PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] How to check file extension using React

 July 30, 2022     file-extension, image, pdf, reactjs, typescript     No comments   

Issue

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

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" ? ...


Answered By - Ryan Le
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing