Node.js 直接从文件系统导入管道文件
我正在节点6.9.0上使用“express”:“^4.13.3” 当我尝试通过管道将数据传输到jpeg图像时:Node.js 直接从文件系统导入管道文件,node.js,express,Node.js,Express,我正在节点6.9.0上使用“express”:“^4.13.3” 当我尝试通过管道将数据传输到jpeg图像时: const path = config.storageRoot + '/' + req.params.originalFileName; var mimetype = mime.lookup(req.params.originalFileName); res.writeHead(200, { 'Content-Type': mimetype}); fs.createReadStre
const path = config.storageRoot + '/' + req.params.originalFileName;
var mimetype = mime.lookup(req.params.originalFileName);
res.writeHead(200, { 'Content-Type': mimetype});
fs.createReadStream(path).pipe(res);
我在结果中获取xml数据:
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.5-c014 79.151739, 2013/04/03-12:12:15 ">
当我将res.end
与fs.readFile
的结果一起使用时,二进制内容的格式正确
我做错了什么?看看我是如何在这个答案的示例中传输文件的:
// 'type' is the MIME type
var s = fs.createReadStream(file);
s.on('open', function () {
res.set('Content-Type', type);
s.pipe(res);
});
s.on('error', function () {
res.set('Content-Type', 'text/plain');
res.status(404).end('Not found');
});
所以我只是将标题设置为由Express发布,而不是显式发布标题。我也在处理流事件。特拉维斯说,也许你应该试着做类似的事情,因为我做这件事的方式似乎很有效:
除了处理流事件和错误之外,另一件事是确保您具有正确的编码、权限等。我不知道您期望的结果是什么,以及XML的含义或来源,但是处理流事件可能会告诉您更多关于正在发生的事情。如果您使用您的解决方案,我仍然会得到相同的结果。我怀疑xml数据是exif数据。