尖锐无法读取文件缓冲区
我正在使用 express-fileupload
用于读取API的文件。现在,我想使用 Sharp
在请求主体中处理图像。
我不想首先将文件保存在服务器上,并使用 fs.ReadFilesync
进行处理。
我尝试传递 req.files.image.data
,该应该是一个缓冲区。
const image = await sharp(Buffer.from(req.files.image.data))
.resize(500, 500)
.jpeg({ quality: 10 })
.toBuffer()
.then((outputBuffer) =>
({ data: outputBuffer, mimetype: 'image/jpeg' }))
.catch(err => {
console.log(err);
return null;
});
但是它正在抛出错误此错误: [错误:vipsjpeg:输入文件的过早末端]
当我尝试将图像缓冲区数据转换为 post ,将其转换为使用 buffer.from
然后通过它,然后传递错误: [error:input buffer包含不支持的映像格式]
edit> edit :有一个:有一个:限制图像大小5MB,这就是为什么大于该图像在缓冲区中没有完全捕获的图像的原因,因此,此错误。
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我看到您的代码时,会出现一些问题。让我尝试通过提出一些问题并提供提示,以更接近可能的解决方案:
req.files.image.data
确实是一个缓冲区,为什么您尝试通过使用再次生成缓冲区buffer.from(req.files.image.data)
?您想从缓冲区创建缓冲区吗?.tobuffer()
进行缓冲区的转换。来自已经存在的缓冲区?在这种情况下,我从个人经验中讲话,如果试图从已经存在的缓冲区创建缓冲区,夏普会出现错误。req.files.image.data
应该是缓冲区。听起来也许您不是100%确定的。我建议检查您是否真的使用const isitreallyBuffer = buffer.isbuffer(req.files.image.data)
之后,您可以简单地将其打印到控制台:
console.log(IsitreallyBuffer); //
req.files.image.data
的类型。它真的是一个未损坏的图像文件吗?它是否真的符合上面列出的夏普接受的输入选项?const image =等待夏普(req.files.image.data)...
.tobuffer()
修正案
fs.ReadFileSync
通常用于处理图像文件。在这种情况下,我从个人经验中谈论了在Node.js中使用图像文件的许多天的经验,我会考虑使用FS,并且更好地更喜欢敏锐的包装来读取和编写图像文件。我不再使用FS。 FS是串联的图像块,进而增加了导致记忆猪的概率。您只需在桌面上使用WordPad或记事本打开PNG图像,然后搜索IDAT块。然后,使用FS软件包处理相同的图像,您将看到差异。突然之间,您可能只有一个非常巨大的IDAT块。
Some questions arise, when I see your code. Let me try to get closer to a possible solution with your provided input by asking some questions and providing hints:
req.files.image.data
is really a buffer, why do you try to generate a buffer again by usingBuffer.from(req.files.image.data)
? You want to create a buffer from a buffer?.toBuffer()
. From an already existing buffer? In this case, I speak from personal experience, sharp would throw an error if trying to create a buffer from an already existing buffer.req.files.image.data
is supposed to be a buffer. Sounds maybe you are not 100% sure. I suggest to check if you really have a buffer by usingconst isItReallyBuffer = Buffer.isBuffer(req.files.image.data)
After that you can simply print it to the console:
console.log(isItReallyBuffer); // true or false
req.files.image.data
one more time. Is it really a not corrupted image file? Does it really comply with the input options accepted by Sharp listed above?const image = await sharp(req.files.image.data)...
.toBuffer()
AMENDMENT
fs.readFileSync
is often used to process image files. In this case, I speak from my personal experience of many days of working with image files in node.js, I would think about using fs and better prefer the Sharp package for reading and writing image files. I don't use fs anymore. Fs is concatenating image chunks which in turn increases the probability leading to memory hogs.You can simply open an PNG image on your desktop with WordPad or Notepad and search for IDAT chunks. Then, process the same image with fs package and you will see the difference; all of a sudden you likely have only one very huge IDAT chunk.