Html 尝试使用内容类型image/jpeg响应时图像损坏
在我的前端html中,我需要一个带有源“/uploads/profilePicture”的Html 尝试使用内容类型image/jpeg响应时图像损坏,html,node.js,image,express,routing,Html,Node.js,Image,Express,Routing,在我的前端html中,我需要一个带有源“/uploads/profilePicture”的img标记。在node/express中处理请求时,我希望对用户进行身份验证,从文件系统加载用户的配置文件图片,并使用图像数据进行响应。我的代码基本上可以归结为: app.get('*', function(req, res, next) { /* Some code leading to the function getProfilePicture */ }); function getProf
img
标记。在node/express中处理请求时,我希望对用户进行身份验证,从文件系统加载用户的配置文件图片,并使用图像数据进行响应。我的代码基本上可以归结为:
app.get('*', function(req, res, next) {
/* Some code leading to the function getProfilePicture */
});
function getProfilePicture(req, res, next) {
loadFile('uploads/' + req.user.local.picture, function(image) {
res.writeHead(200, { 'Content-Type': 'image/jpeg' });
res.end(image);
});
};
运行代码时,前端会显示一个中断的图像,但控制台中不会打印任何错误
有趣的是,这以前对我很有效。不久前,我的路由代码没有那么通用,因为它对我网站上的每个页面都使用了app.get()
。所以我使用了app.get('/uploads/profilePicture')
而不是app.get('*')
。这可能是问题所在吗
编辑:
我正在使用节点v0.10.35和express 3.0.6
编辑2:
使用Express 3.19.1进行了测试,但仍然不起作用。结果表明,使用确实起到了作用
res.status(200).sendfile(URL);
请解释为什么sendfile可以工作,而不是setHeaders+end,我们将不胜感激