Detecting file type from buffer in node js?

耗尽温柔 提交于 2020-07-21 03:32:56

问题


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

回答1:


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]



来源:https://stackoverflow.com/questions/57406501/detecting-file-type-from-buffer-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!