跳到内容
🛠️ToolsShed

Base64文件解码器

将Base64字符串解码为可下载的文件。

关于此工具

Base64文件解码对于处理编码数据的开发者、设计师和技术专业人员来说是一项至关重要的操作。当二进制文件——图像、PDF、存档或文档——被转换为Base64文本格式时,它们便于通过API传输、通过电子邮件发送,或直接存储在JSON数据库中。此工具的存在就是为了快速方便地完成这个反向过程,让你无需终端命令或复杂编程知识即可从编码表示中下载原始文件。

在实践中,你会在许多场景中遇到Base64编码的文件:Web API将图像或文档作为Base64字符串返回、嵌入在HTML或CSS中的数据URL、在MIME消息中编码的电子邮件附件,或将二进制数据存储为Base64的区块链应用和数据库。工作流程很简单——粘贴你的Base64字符串(或数据URL),从MIME下拉菜单中选择正确的文件类型,为文件命名,然后立即下载。该工具完全在浏览器中处理解码,因此你的数据永远不会离开你的设备。

无论你是从JSON API响应中提取图像、从数据URL恢复文件,还是在数据库中调试编码内容,此工具都能消除常见技术任务的麻烦。它针对偶尔遇到Base64的普通用户和定期处理编码文件的专业人士而设计,提供了在线转换器或手动解码脚本的快速、可靠的替代方案。

常见问题

代码实现

import base64

def decode_base64_file(b64_string: str, output_path: str) -> None:
    """Decode a base64 string (with or without data URL prefix) and write to file."""
    # Strip data URL prefix if present: data:image/png;base64,...
    if "," in b64_string:
        b64_string = b64_string.split(",", 1)[1]
    # Remove whitespace
    b64_string = b64_string.strip().replace("\n", "").replace("\r", "")
    data = base64.b64decode(b64_string)
    with open(output_path, "wb") as f:
        f.write(data)
    print(f"Decoded {len(data)} bytes → {output_path}")

# Encode a file to base64
def encode_file_to_base64(path: str) -> str:
    with open(path, "rb") as f:
        return base64.b64encode(f.read()).decode("utf-8")

# Round-trip example
encoded = encode_file_to_base64("example.png")
decode_base64_file(encoded, "decoded_example.png")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.