javascript - 从 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 无法解析它,所以问题是为什么?
解决方案
您可以包含这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 文件的通用签名开始?在这里找到它们
推荐阅读
- python - Taking input for dictionary in python
- javascript - 第一个 AdSense 广告随机展示(异步)
- python - 如何将一个文本文件中的多列替换为另一个文本文件中的列?
- javascript - 帆 JS。无法从 POST 表单“创建”
- class - 类从哪里来?
- ionic-framework - cordova build android 在 ionic 中不起作用
- php - 如何保持 MongoDB 与源相同?(拉拉维尔)
- scala - 在 SparkML Transformer 中缓存数据集
- python - 在 Python 中将主列表拆分为多个字典键和值
- google-chrome-devtools - 将本地文件系统上的源映射应用到 Chrome DevTools 中的精简生产代码