想要通过图像名称来展示从数据库获取的图像。

huangapple go评论109阅读模式
英文:

Want to show the image that i get from db as by its name

问题

Your provided code is in English, but you've requested a translation. Here's the translated code:

问题是我上传的图像被设置在数据库中,并保存在VS Code的uploads文件夹中,但我不知道如何在前端显示它们。我尝试了一些方法,但需要进一步了解该过程。

  1. <script>
  2. axios
  3. .get("http://localhost:8000/hotels")
  4. .then((response) => {
  5. console.log(response.data)
  6. for (let i = 0; i < response.data.length; i++) {
  7. document.getElementById('data').innerHTML += `<tr class="odd">
  8. <td class="dtr-control sorting_1" tabindex="0">${response.data[i].id}</td>
  9. <td><img src='${response.data[i].image}'></td>
  10. <td>${response.data[i].name}</td>
  11. <td>${response.data[i].description}</td>
  12. <td>${response.data[i].price}</td>
  13. <td>${response.data[i].address}</td>`;
  14. }
  15. })
  16. .catch((error) => {
  17. console.log(error);
  18. });
  19. </script>

在这里,您可以看到,当我上传数据时,图像存储在uploads文件夹中,我们只需找到一种方法在前端显示图像即可。

英文:

SO my problem is images that i upload are set in the database and saves in vs code in uploads folder but i dont know how to display them on front end i have tried something but furthur needs to know the procedure

  1. &lt;script&gt;
  2. axios
  3. .get(
  4. &quot;http://localhost:8000/hotels&quot;)
  5. .then((response) =&gt; {
  6. console.log(response.data)
  7. for (let i = 0; i &lt; response.data.length; i++) {
  8. document.getElementById(&#39;data&#39;).innerHTML+=`&lt;tr class=&quot;odd&quot;&gt;
  9. &lt;td class=&quot;dtr-control sorting_1&quot; tabindex=&quot;0&quot;&gt;${response.data[i].id}&lt;/td&gt;
  10. &lt;td&gt;&lt;img src=&#39;${response.data[i].image}&#39;&gt;&lt;/td&gt;
  11. &lt;td&gt;${response.data[i].name}&lt;/td&gt;
  12. &lt;td&gt;${response.data[i].description}&lt;/td&gt;
  13. &lt;td&gt;${response.data[i].price}&lt;/td&gt;
  14. &lt;td &gt;${response.data[i].address}&lt;/td&gt;`
  15. }
  16. })
  17. .catch((error) =&gt; {
  18. console.log(error);
  19. });
  20. &lt;/script&gt;

So here you can see when i upload data the images are stored in the uploads folder All we need is to display image on front end somehow

答案1

得分: 1

您的/uploads文件夹不像其他路由一样被托管。您需要托管它以解决URL。

编辑:解释
在您的app.js/index.js(您已初始化app()的位置)中编写以下代码:

  1. const path = require('path')
  2. app.use('/uploads',
  3. express.static(path.join(__dirname,
  4. 'uploads')))

我也认为这个问题类似于如何使用Node.js提供图像的问题。

英文:

Your /uploads folder is not hosted like other routes. you need to host it in order to resolve the URL.
Here is how you can using express.

EDIT: Explanation
In your app.js/index.js(where you have initialized app())
write the code below

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const path = require(&#39;path&#39;)
  2. app.use(&#39;/uploads&#39;,
  3. express.static(path.join(__dirname,
  4. &#39;uploads&#39;)))

<!-- end snippet -->
Also i believe the question is similar to How to serve an image using nodejs

答案2

得分: -3

你可以使用以下代码替代:

  1. &lt;td&gt;&lt;img src=&#39;${new URL(&quot;uploads/&quot;+response.data[i].image, window.location.origin).toString()}&#39;&gt;&lt;/td&gt;
英文:

You can use instead of

  1. &lt;td&gt;&lt;img src=&#39;${response.data[i].image}&#39;&gt;&lt;/td&gt;

this line

  1. &lt;td&gt;&lt;img src=&#39;${new URL(&quot;uploads/&quot;+response.data[i].image, window.location.origin).toString()}&#39;&gt;&lt;/td&gt;

huangapple
  • 本文由 发表于 2023年1月5日 18:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75016825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定