+
82
-

回答

直接通过这个html代码实现base64编码转视频二进制下载

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Base64 数据输入与下载</title>
</head>

<body>
    <!-- textarea 用于输入 Base64 数据 -->
    <textarea id="base64Input" rows="10" cols="50" placeholder="请输入 Base64 编码的数据"></textarea>
    <br>
    <!-- 按钮用于触发下载操作 -->
    <button id="downloadButton">下载文件</button>

    <script>
        const base64Input = document.getElementById('base64Input');
        const downloadButton = document.getElementById('downloadButton');

        downloadButton.addEventListener('click', () => {
            const base64Data = base64Input.value;
            if (!base64Data) {
                alert('请输入有效的 Base64 数据');
                return;
            }

            try {
                // 提取 Base64 数据部分,去除前缀
       
                // 将 Base64 转换为二进制数据
                const binaryString = window.atob(base64Data);
                const binaryLength = binaryString.length;
                const bytes = new Uint8Array(binaryLength);
                for (let i = 0; i < binaryLength; i++) {
                    bytes[i] = binaryString.charCodeAt(i);
                }

                // 创建 Blob 对象
                const blob = new Blob([bytes], { });

                // 创建下载链接
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                // 根据 MIME 类型生成默认文件名
               
                    fileName = 'downloaded_file.mp4';
               
                a.download = fileName;
                a.style.display = 'none';
                document.body.appendChild(a);

                // 触发下载
                a.click();

                // 释放临时 URL
                URL.revokeObjectURL(url);
                document.body.removeChild(a);
            } catch (error) {
                alert('输入的 Base64 数据无效,请检查后重试。');
            }
        });
    </script>
</body>

</html>

网友回复

我知道答案,我要回答