最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detecting file type from buffer in node js? - Stack Overflow

programmeradmin2浏览0评论

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)
    });
});

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)
    });
});
Share Improve this question edited Jan 31, 2020 at 17:31 ns16 1,5502 gold badges20 silver badges29 bronze badges asked Aug 8, 2019 at 6:36 TechChainTechChain 8,94432 gold badges115 silver badges243 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14

Have a look at this: https://github.com/sindresorhus/file-type/ . If you want to know how it works, I think the code is at https://github.com/sindresorhus/file-type/blob/master/index.js

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]

发布评论

评论列表(0)

  1. 暂无评论