从节点js的缓冲区中检测文件类型? [英] Detecting file type from buffer in node js?
本文介绍了从节点js的缓冲区中检测文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经从可以是pdf,jpg或任何其他格式的文件创建了缓冲区。现在,我要检测缓冲区是否为pdf文件或任何其他文件。
I have created a buffer from a file which can be pdf, jpg or any other format. Now I want to detect if the buffer is of the pdf file or any other file.
request({ url, encoding: null }, (err, resp, buffer) => {
hashFromFilebuffer('sha256', buffer).then(function (result) {
console.log(result)
}).catch(function (error) {
console.log(error)
});
});
推荐答案
看看这个:> https://github.com/sindresorhus/file-type/
。
如果您想知道它是如何工作的,我认为代码位于 https://github.com/sindresorhus/file-type/blob/master/index.js
从源代码中,如果第一个字节是
[0x25、0x50、0x44、0x46]
,则该文件似乎是pdf,如果第一个字节是 [0xFF,0xD8、0xFF]
From the code in the source, it appears that the file is a pdf if the first bytes are
[0x25, 0x50, 0x44, 0x46]
, and is a jpg if the first bytes are [0xFF, 0xD8, 0xFF]
这篇关于从节点js的缓冲区中检测文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文