회원사진 업로드시 미리보기 또는 바로 저장방법은 어떻게 해야하나요? 채택완료

그누보드 회원사진의 경우 업로드시 파일명만 기재되고 따로 저장버튼을 클릭해야 업로드가 되는데요

혹시 이걸 업로드시 미리보기와 함께 자동저장하려면 어떤방법으로 사용해야할까요?

답변 3개

채택된 답변
+20 포인트

ajax 파일업로드 플러그인이 있습니다. 한번 알아보시고 파일 선택하고 나서 해당을 실행해서 파일만 미리 저장하시면 될거 같습니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

http://foliotek.github.io/Croppie/

 

이런 라이브러리와 연동하여  이미지 업로드 파트를 구현하면 됩니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

Copy
<!DOCTYPE html>
<html lang="en">
    <head>
        <style>
        #preview {
            background-color: #eee;
            height: 10em;
            margin: 1em 0;
        }
        #preview img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
        </style>
        <script>
        document.addEventListener('DOMContentLoaded', function () {
            const preview = document.getElementById('preview');
            const attach = document.getElementById('attach');
            const submit_ajax = document.getElementById('submit_ajax');

 

            // preview
            attach.addEventListener('change', function (evt) {
                const [file] = this.files;
                const url = URL.createObjectURL(file);
                preview.innerHTML = '<img src="' + url + '" />';
            });

 

            // upload
            submit_ajax.addEventListener('click', function (evt) {
                const [file] = attach.files;
                if (file != null) {
                    const action = location.pathname; // https://domain.com/path/file.php
                    const formData = new FormData();
                    formData.set('attach', file, file.name);
                    fetch(action, {
                        method: 'POST',
                        body: formData
                    })
                    .then(response => response.text())
                    .then(data => {
                        alert('upload complete');
                    });
                }
            });
        }, false);
        </script>
    </head>
    <body>
        <div id="preview"></div>
        
        <input type="file" accept="image/*" name="attach" id="attach" />
        <button type="button" id="submit_ajax">Ajax submit</button>
    </body>
</html>
로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

위 처럼 사진을 넣으면, 어디로 저장이 되는건가요?

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고