首页 > 解决方案 > 从 base64 图像中获取 mime 和 ext

问题描述

我有下一个代码:

const buffer = Buffer.from(image, 'base64');
const { ext, mime } = await FileType.fromBuffer(buffer);

但它失败并出现下一个错误:

UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'ext' of '(intermediate value)' as it is undefined.

图像只是 JPG 文件,我可以在照片编辑器中轻松打开。但是由于某种原因 FileType 无法解析它,所以问题是为什么?

标签: javascriptnode.js

解决方案


您可以包含这undefined意味着不匹配,但我不得不在这里查找它,而不是假设这确实是您正在使用的包。

    @param buffer - A buffer representing file data. It works best if the buffer contains the entire file, it may work with a smaller portion as well.
    @returns The detected file type and MIME type, or `undefined` when there is no match.
    */
    function fromBuffer(buffer: Buffer | Uint8Array | ArrayBuffer): Promise<core.FileTypeResult | undefined>;

看看你用某种十六进制阅读器或哎呀创建缓冲区的文件,看看你在调试中创建的缓冲区,它是否以 jpeg 文件的通用签名开始?在这里找到它们


推荐阅读