简体   繁体   English

如何在 React 中显示图像,这些图像作为文件下载并保存在服务器上的文件夹中,文件路径存储在我的数据库中?

[英]How do I Display Images in React which are downloaded as Files & saved in a Folder on the Server with the Filepath stored in my Database?

Short Setup, Problem & Code Summary简短设置、问题和代码摘要

Setup设置

React.js, Node.js/Express.js & PostgreSQL. React.js、Node.js/Express.js 和 PostgreSQL。

I scrape an ImageUrl of a Book, download and store it in a File in an Image Folder with Node.js and store the Filepath in my postgreSQL Database AND I would like to fetch and display the Image in an <img /> tag on my React frontend.我刮了一本书的ImageUrl,下载并使用Node.js将其存储在图像文件夹中的文件中,并将文件路径存储在我的postgreSQL数据库中,我想在我的React前端的<img />标签中获取并显示图像.

Problem问题

The Image I Fetch from my Database through my API doesn't get displayed, eventhough I can console.log the Image Object in the devtool-console and even make a succesful Postman request with the Image-Object as JSON response. The Image I Fetch from my Database through my API doesn't get displayed, eventhough I can console.log the Image Object in the devtool-console and even make a succesful Postman request with the Image-Object as JSON response. Furthermore I can get the image even be displayed if I go to localhost:3001/image-193x278.png.此外,如果我将 go 转到 localhost:3001/image-193x278.png,我什至可以显示图像。 My <img /> Tag has even the right Img src attribute displayed in the devtools (images\image-193x278.png)我的<img />标签甚至在 devtools (images\image-193x278.png) 中显示了正确的 Img src 属性

In the devtool-console I get the following error: GEThttp://localhost:3002/images/image-193x278.png [HTTP/1.1 404 Not Found 0ms]在 devtool-console 我收到以下错误: GEThttp://localhost:3002/images/image-193x278.png [HTTP/1.1 404 Not Found 0ms]

(Note: My Frontend runs on localhost 3002 while my server & frontend are running concurrently with a proxy on 3001, but that isn't the causing Problem here) (注意:我的前端在 localhost 3002 上运行,而我的服务器和前端与 3001 上的代理同时运行,但这不是导致问题的原因)

Code代码

React Code反应代码

const [image, setImage] = useState([]);

  useEffect(() => {

    async function fetchData() {
      const response = await fetch('http://localhost:3001/test', {
        method: 'GET',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        }
      })
      const data = await response.json();
      console.log(data);
      setImage(data);
    }
    fetchData();

  }, []);

I tried to get it displayed in two different ways:我试图让它以两种不同的方式显示:

<img className={css(styles.Image)} src={image.test_images} alt="" />

{image.map((img) => 
<img className={css(styles.Image)} src={img.test_images} alt="" />)}

Server-Side服务器端

app.get('/test', async (req, res) => {
    try {
        const image = await testdb.query('SELECT * FROM test_table');
        res.json(image.rows);
    } catch (err) {
        console.error(err);
    }
})

I serve an Express Static File我提供 Express Static 文件

app.use(express.static('images'));

I even tried two additional varients just in case我什至尝试了另外两个变种以防万一

app.get('/test', express.static(__dirname + '../images'));
app.use(express.static('../images'));

Still with no succes.仍然没有成功。 I hope i could make myself understood as good as possible and I would be extremely thankful for every help thrown out my way.我希望我能尽可能地让自己被理解,我会非常感谢每一个帮助我的人。

The problem is with your static folder.问题在于您的 static 文件夹。 The static files will be available at localhost:3002/:filename . static 文件将在localhost:3002/:filename可用。

If you want to make it work under /images path, then you can add the path to the static declaration like如果你想让它在/images路径下工作,那么你可以将路径添加到 static 声明中,例如

app.use('/images', express.static('images'))

Then you can hit the localhost:3002/images/:filename .然后你可以点击localhost:3002/images/:filename

You can find the documentation here: https://expressjs.com/en/starter/static-files.html您可以在此处找到文档: https://expressjs.com/en/starter/static-files.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何预览存储在服务器文件夹中的网页上的图像、pdf、视频文件? - how to preview images , pdf , video files on my web page stored in a folder on my server? 如何访问我的 React 项目的公共文件夹中的图像? - How do I access images in the public folder of my React project? 如何显示数据库中的随机图像? - How do I display random images from my database? 如何在React中导入图像文件夹,文件中的每个独立图像都可以状态存储? - How do I import a folder of images in React where each indvidual image in the file can be stored in state? 如何访问保存在我的 Express 服务器上的文件? - How do I access my files saved onto my express server? 我尝试在 p5.js 中保存 27 张图片,但只有 10 张保存到我的下载文件夹中。 为什么所有文件都没有保存? - I tried to save 27 images in p5.js but only 10 is saved into my downloads folder. Why are all the files not saved? 如何允许将文件下载到 Downloads 文件夹以外的文件夹? - How do I allow files to be downloaded to a folder other than the Downloads folder? 如何检查我的图像是否保存在Heroku服务器上 - How to check if my images are saved on Heroku server 如何从文件夹中选取图像并以 HTML 格式显示? - How do I pick images from folder and display in HTML? 如何读取文件夹图像并在html中显示? - How do I Read folder images and display inside html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM