前端例程:十六进制数组转文件工具

前言

之前写过 《前端例程:文件转十六进制数组工具》 这个文章。后面有人有逆转此过程的需求,所以就写了这个工具。

功能演示

在这里插入图片描述

代码实现

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>十六进制字节数组转文件工具</title>
    <script>
        function clean() {
      
      
            document.getElementById("txtarr").value = "";
        }

        function download() {
      
      
            let txtarr = document.getElementById("txtarr").value.split(",");
            let filearr = new Uint8Array(txtarr.length);
            for (let i = 0; i < txtarr.length; i++) {
      
      
                filearr[i] = parseInt(txtarr[i].replaceAll(" ", ""));
            }

            let file = new Blob([filearr], {
      
       type: "application/octet-stream" });

            let downloadlink = document.createElement("a");
            downloadlink.href = URL.createObjectURL(file)
            downloadlink.download = document.getElementById("filename").value;
            downloadlink.click();
        }
    </script>
</head>

<body>
    <p>十六进制字节文件数据粘贴到下面:</p>
    <textarea id="txtarr" style="overflow-y: scroll; width: 30rem; height: 15rem;"></textarea>
    <br><br><button onclick="clean()">清空数据</button>
    <br><br><span>文件名:</span><input type="text" id="filename"> <button onclick="download()">下载文件</button>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/Naisu_kun/article/details/123762941