简体   繁体   中英

How to show the images of card components that exist outside the src folder in ReactJs?

I have a react app which integrated with FastAPI and MongoDB. In my project, there is a page to show some information as cards. These cards contain images, but those images are generated automatically in the backend folder. Each card contains the path of its own picture, but when I call this path in the component, it doesn't show the picture as illustrated in the picture 1 . Note: I used React component class format to build my project!

在此处输入图片说明

Here is how each card is generated:

    renderCard(card, index) {
    return (
      <Card key={index} className="box">
        <Card.Img className="bodyImg" variant="top" src={card.coverPic} />
        <Card.Body className="bodytext">
          <Card.Title>{card.name}</Card.Title>
          <Card.Text>{card.Placename}</Card.Text>
          <Card.Text>{card.setting}</Card.Text>
          <Link
            to={{
              pathname: "/CardInfo",
              state: card,
            }}
            style={{ textDecoration: "none" }}
            className="buttonStyle"
          >
            Show
          </Link>
        </Card.Body>
      </Card>
    );
  }

Here is the information for two cards:

First card:

{
  "_id": {
    "$oid": "60fd0c3b3d4412b25bea49e7"
  },
  "name": "Test the coverPic",
  "path": [
    "C:/Users/Asma/Desktop/Projects/backend/app/videos/HaramCovid2.mp4"
  ],
  "Placename": "Makkahh",
  "setting": "Public Place",
  "country": "Saudi Arabia",
  "duration": "1",
  "date": "2021-06-29",
  "sendEmail": true,
  "publish": true,
  "outputVideoPath": "output/Test the coverPic.avi",
  "contactRate": 1,
  "userId": "60fbffa2e7db6bc44a842ea6",
  "framesPath": "C:/Users/Asma/Desktop/Projects/backend/app/videos/output/",
  "coverPic": "C:/Users/Asma/Desktop/Projects/backend/app/videos/output/Test the coverPic-30.png"
}

Second card:

{
  "_id": {
    "$oid": "60fc79bbac4f82a891c01ef2"
  },
  "name": "testing",
  "path": [
    "C:/Users/Asma/Desktop/Projects/backend/app/videos/HaramCovid2.mp4"
  ],
  "Placename": "Haram",
  "setting": "Mass Gathering",
  "country": "Åland Islands",
  "duration": "1",
  "date": "2021-07-02",
  "sendEmail": false,
  "publish": false,
  "outputVideoPath": "output/testing.avi",
  "contactRate": 1,
  "userId": "60fc79a0ac4f82a891c01ef1",
  "framesPath": "C:/Users/Asma/Desktop/Projects/backend/app/videos/output/",
  "coverPic": "output/testing-30.png"
}

AFAIK you cannot use files from outside the src folder. I guess that has some security reasons. You could try to create a symlink or hardlink. It's a link to another file or directory that can be created inside your src folder.

Open a cmd window and navigate to the folder of your choice inside your src folder, eg images .

Execute mklink /h HaramCovid2.mp4 "C:/Users/Asma/Desktop/Projects/backend/app/videos/HaramCovid2.mp4" to create a hard link.

JSON:

  "path": ["./images/picture1"],

EDIT: Changed the mklink command. Please try this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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