选择图片转换Base64 结果显示图像和编码 可复制Base64编码

chenyajun  2022-10-16 13:51:35  阅读 1877 次 评论 0 条
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">


    <title>Free Classified Ads in Pakistan</title>
    <!-- links -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B"
        crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
        crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Kanit" rel="stylesheet">
</head>

<body>


    <br>
    <div>
        <div>
            <div class="col-md-6 m-auto">
                <h1 class="text-center display-4 my-4">
                    上传照片
                </h1>
                <div class="custom-file mb-3">
                    <input type="file" name="filename" id="inputFileToLoad">
                    <label for="file">选择文件</label>
                </div>
                <input type="text" id="response">
                <br>
                <input type="submit" value="提交" class="btn btn-primary btn-block" id="BaseToImage">

            </div>
        </div>
    </div>

    <br>

    <section style="margin-left: 29%;">
        <div>
            <div>
                <div>
                    <img src="" id="preview" style="display: none; width: 500px; height: 500px;">
                </div>
            </div>
        </div>
    </section>
    <br>
    <br>

    </div>
    </div>
    </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em"
        crossorigin="anonymous"></script>
<!--    <script src="./Base64.js"></script> -->
<script language="JavaScript" type="text/javascript">
//Base64.js代码
$(document).ready(function () {
    $("#BaseToImage").click(function () {
        document.getElementById("preview").setAttribute('src', $("#response").val());
        $("#preview").show();
    });
});



$(document).ready(function () {
    $("#inputFileToLoad").change(function () {
        var fileSelected = document.getElementById("inputFileToLoad").files;
        if (fileSelected.length > 0) {
            var fileToLoad = fileSelected[0];
            var fileReader = new FileReader();
            fileReader.onload = function (fileLoadedEvent) {
                var base64value = fileLoadedEvent.target.result;
                console.log(base64value);
                $("#response").val(base64value);
            };
            fileReader.readAsDataURL(fileToLoad);
        }
    });
});

</script>

        
</body>

</html>


本文地址:https://chenyajun.net/index.php/post/102.html
版权声明:本文为原创文章,版权归 chenyajun 所有,欢迎分享本文,转载请保留出处!

评论已关闭!