【发布时间】:2018-11-10 04:45:01
【问题描述】:
我正在尝试从node.js 获取图像(jpg 文件)并将其显示在 html 标记(img)中,但图片未显示(如您所见:)。
我处理请求的 node.js 看起来:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.get('/pictureItem', function (req, res) {
// imagesNames[0].name - contains the name of the image to send (jpg file)
res.sendFile('/images/' + imagesNames[0].name, {root: __dirname })
})
我的 js 代码看起来:
$.get("/pictureItem", function(data, status)
{
console.log("got image");
$("#imageId").attr("src", data);
})
我错过了什么?
【问题讨论】:
-
返回的
data的值是多少?看起来您正在返回文件本身的字节数据 -
您根据要求发送的文件是什么?它是
.html文件吗?你能指定你的前端js代码在哪里吗 -
如果 Taplar 推测您正在发送图像的字节数据,那么您做错了!显示图像所需要做的就是使用新的图像元数据更新 dom,浏览器将自行获取并渲染图像。即使您确实想要字节数据,比如绘制到 html5 画布中,看起来您的客户端“获取”请求实际上并不是在请求文件。没有迹象表明它填充了“imageNames”参数。反过来,看起来 imageNames 参数应该在正文中,这不是 GET 请求的标准过程!
标签: javascript jquery html node.js