【问题标题】:display images with fs.readfile in NodeJS [duplicate]在 NodeJS 中使用 fs.readfile 显示图像 [重复]
【发布时间】:2020-06-20 13:14:33
【问题描述】:

我有一个为自制框架加载 css 的简单脚本(我没有使用 express,而只是使用 http) 我的功能如下:

function getStatic(req, res) {

    console.log(req.params);
    if (fs.existsSync('./public/' + req.params.file)) {

        var file = fs.readFileSync('./public/' + req.params.file, 'utf8');


        res.write(file);
        res.end();

    } else {
        res.writeHead(200, {
            "Content-Type": "text/html; charset=utf-8"
        });
        res.write("⚽⚽⚽⚽ 404 - not found ⚽⚽⚽⚽");
        console.log(req);
        res.end('error')
        console.log(req.params);

    }

}

不幸的是,图像以文件下载的形式呈现。我在这里找到了类似的答案nodejs - How to read and output jpg image?,但我不确定如何将它集成到我的代码中以涵盖 jpg、png 和可能的其他图像类型?

【问题讨论】:

标签: javascript node.js


【解决方案1】:

您必须设置 content-type 以便浏览器可以正确处理请求的文件。

顺便说一句,有一个本地文件包含漏洞,您可以在那里阅读更多信息:https://en.wikipedia.org/wiki/File_inclusion_vulnerability

【讨论】:

  • 感谢您提供的信息,所以我认为我需要使用不同的方法
猜你喜欢
  • 2014-02-09
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2019-03-10
  • 2017-12-24
相关资源
最近更新 更多