이미지 미리보기 업로드

Web Development assistant·2022년 4월 3일
0

# javascript

목록 보기
20/36
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function () {
            $('input[name="file_path"]').change(function () {
                console.log(this);
                setImageFromFile(this, '#preview');
            });

            function setImageFromFile(input, expression) {
                if (input.files && input.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $(expression).attr('src', e.target.result);
                    }
                    reader.readAsDataURL(input.files[0]);
                }
            }
        });
    </script>
</head>

<body>
    <img src="" id="preview" style="width: 50%; height: 50%;"/>
    <input type="file" name="file_path" class="files" style="width: 270px; height: 46px;">
</body>

</html>

0개의 댓글