admin管理员组文章数量:1025457
I can't find a simple solution how to display an image on my website. I can read it in my node js backend but it will download the file instead of placing in my img tag.
Do you have a simple solution for that? Thank you very much!
HTML
let cell6 = document.createElement("td");
cell6.innerHTML = `<img src={http://localhost:4001/getImage/pexels-cottonbro-4065175.jpg}></img>`;
NODE JS
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.send(image);
}
);
};
I can't find a simple solution how to display an image on my website. I can read it in my node js backend but it will download the file instead of placing in my img tag.
Do you have a simple solution for that? Thank you very much!
HTML
let cell6 = document.createElement("td");
cell6.innerHTML = `<img src={http://localhost:4001/getImage/pexels-cottonbro-4065175.jpg}></img>`;
NODE JS
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.send(image);
}
);
};
Share
Improve this question
asked Feb 2, 2022 at 10:50
jansemraujansemrau
2511 gold badge3 silver badges15 bronze badges
5
-
I can't see how that code would generate a download. If you assign a value to
src
it will either display it as an image or it will discard the resource as invalid. It won't download it. The cause must be outside the code you shared. You need to provide a minimal reproducible example. – Quentin Commented Feb 2, 2022 at 10:59 - Are you able to place the file in a static folder such as public?and serve the folder using express? – Quatban Taco Commented Feb 2, 2022 at 11:00
- 1 Do you mean serve an image ? – ikhvjs Commented Feb 2, 2022 at 11:01
- @ikhvjs , that should help. – Quatban Taco Commented Feb 2, 2022 at 11:02
- express.static works for me. Thank you very much! – jansemrau Commented Feb 2, 2022 at 11:10
1 Answer
Reset to default 4The problem you face here is not how you read the data, it's how you send the data to Frontend.
First of all, you need to set the headers properly that the frontend (receiver) understands that it's an image and doesn't download that but to show it.
Modified your code here:
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.setHeader('Content-Type', 'image/jpg');
res.setHeader('Content-Length', ''); // Image size here
res.setHeader('Access-Control-Allow-Origin', '*'); // If needs to be public
res.send(image);
}
);
};
I can't find a simple solution how to display an image on my website. I can read it in my node js backend but it will download the file instead of placing in my img tag.
Do you have a simple solution for that? Thank you very much!
HTML
let cell6 = document.createElement("td");
cell6.innerHTML = `<img src={http://localhost:4001/getImage/pexels-cottonbro-4065175.jpg}></img>`;
NODE JS
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.send(image);
}
);
};
I can't find a simple solution how to display an image on my website. I can read it in my node js backend but it will download the file instead of placing in my img tag.
Do you have a simple solution for that? Thank you very much!
HTML
let cell6 = document.createElement("td");
cell6.innerHTML = `<img src={http://localhost:4001/getImage/pexels-cottonbro-4065175.jpg}></img>`;
NODE JS
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.send(image);
}
);
};
Share
Improve this question
asked Feb 2, 2022 at 10:50
jansemraujansemrau
2511 gold badge3 silver badges15 bronze badges
5
-
I can't see how that code would generate a download. If you assign a value to
src
it will either display it as an image or it will discard the resource as invalid. It won't download it. The cause must be outside the code you shared. You need to provide a minimal reproducible example. – Quentin Commented Feb 2, 2022 at 10:59 - Are you able to place the file in a static folder such as public?and serve the folder using express? – Quatban Taco Commented Feb 2, 2022 at 11:00
- 1 Do you mean serve an image ? – ikhvjs Commented Feb 2, 2022 at 11:01
- @ikhvjs , that should help. – Quatban Taco Commented Feb 2, 2022 at 11:02
- express.static works for me. Thank you very much! – jansemrau Commented Feb 2, 2022 at 11:10
1 Answer
Reset to default 4The problem you face here is not how you read the data, it's how you send the data to Frontend.
First of all, you need to set the headers properly that the frontend (receiver) understands that it's an image and doesn't download that but to show it.
Modified your code here:
const fs = require("fs");
require("dotenv").config();
module.exports = (req, res) => {
fs.readFile(
`../backend/images/${req.params.id}`,
function (err, image) {
if (err) {
throw err;
}
console.log(image);
res.setHeader('Content-Type', 'image/jpg');
res.setHeader('Content-Length', ''); // Image size here
res.setHeader('Access-Control-Allow-Origin', '*'); // If needs to be public
res.send(image);
}
);
};
本文标签: javascriptHow to easily display Image from Node JSStack Overflow
版权声明:本文标题:javascript - How to easily display Image from Node JS - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745628920a2160038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论