从 Base64 编码的字符串中检索 MIME 类型 [英] Retrieve MIME type from Base64 encoded String

查看:42
本文介绍了从 Base64 编码的字符串中检索 MIME 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个用 Base64 字符串编码并提供给我的文件(例如 myfile.jpeg).我无法知道文件类型是什么.我想将字符串解码为文件(本例中为图像).我如何知道文件的类型(例如 jpeg)?

Let' say a file (e.g. myfile.jpeg) encoded in Base64 String and given to me. There is no way I know what the file type was. I'd like to decode the string into a file (an image in this example). How do I know the type of the file (e.g jpeg)?

推荐答案

一般来说,base 64 编码的字符串绝对可以包含任何数据,因此无法知道它的文件类型.

In general, a base 64-encoded string could contain absolutely any data, so there is no way to know its file type.

要确定它是否是 JPEG 图像的实例,您需要对其进行 base64 解码,然后执行诸如检查其 幻数,这有助于告诉您文件不是.您仍然需要做更多的工作来确定它是否是有效的 JPEG 图像.

To determine if it is an instance of a JPEG image, you'd need to base64-decode it, and then do something like checking its magic number, which is useful in telling you what the file isn't. You'd still need to do more work to determine if it is a valid JPEG image.

这篇关于从 Base64 编码的字符串中检索 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆