AJAX를 사용하여 파일 업로드 하기

<?php

include_once(‘./_common.php’);

include G5_PATH.’/header.php’;

?>

<!DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8″>

<title>AJAX를 사용하여 파일 업로드 하기</title>

</head>

<body>

<script src=”https://code.jquery.com/jquery-latest.js”></script>

<script>

    function file_frm_submit(frm) {

        var fileCheck = frm.upload_file.value;

        if(!fileCheck) {

            alert(“업로드할 파일을 선택하세요.”);

            return false;

        }

        var formData = new FormData(frm);            // 파일전송을 위한 폼데이터 객체 생성    

        formData.append(“message”, “ajax로 파일 전송하기”);

        formData.append(“file”, jQuery(“#upload_file”)[0].files[0]);

        $.ajax({

                url: ‘./ajax_file_upload.php’,

                type        : ‘POST’,

                dataType    : ‘html’,

                enctype        : ‘multipart/form-data’,

                processData    : false,

                contentType    : false,

                data        : formData,

                async        : false,

                cache        : false,

                timeout        : 600000,

                success: function(data){

                    var text_reserlt = data;

                    if( text_reserlt == “MYPAGE” )

                    {

                        console.log(‘mypage’);

                        location.href = ‘/rudtjdtlr/mypage/’;

                    }

                    else if( text_reserlt == “I” )

                    {

                        location.href = ‘/user/auto_login/index.php?id=’+email;

                    }

                    else {

                        console.log(text_reserlt);

                    }

                }

            });

    }

</script>

<form id=”file_frm” enctype=”multipart/form-data”>

    <input type=”text” name=”what” value=”a”>

    <br><br>

    <input type=”file” name=”upload_file” id=”upload_file”>

    <br><br>

    <input type=”file” name=”upload_file2″ id=”upload_file2″>

    <br><br>

    <input type=”file” name=”upload_file3″ id=”upload_file3″>

    <br><br>

    <button type=”button” name=”upload_btn” onClick=”file_frm_submit(this.form);”>ajax 업로드</button>

</form>

</body>

</html>

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다