Ajax 파일 업로드 샘플 코드

by 조쉬 posted Mar 11, 2017
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

FormData를 이용한 JQuery Ajax 파일 업로드 샘플


// form 값 전부 보내기

$('.btn_upfile').click(function(){

  

    var idx = $(this).parent().parent().index();

    var frm = document.getElementById('frm_store');

    frm.method = 'POST';

    frm.enctype = 'multipart/form-data';

  

    var fileData = new FormData(frm);

  

    // ajax

    $.ajax({

        url:'/intra_tmp/backend/image_upload.php',

        type:'POST',

        data:fileData,

        async:false,

        cache:false,

        contentType:false,

        processData:false

    }).done(function(response){

        alert(response);

    });

  

});

  

// 필요한 값만 보내기

$('.btn_upfile').click(function(){

  

    var idx = $(this).parent().parent().index();

    var frm = document.getElementById('frm_store');

    frm.method = 'POST';

    frm.enctype = 'multipart/form-data';

  

    var shopNo = frm.shop_No.value;

    var files = $('#t_img_manager tr:eq('+idx+') .frm_file')[0].files[0];

    var fileData = new FormData();

  

    fileData.append('shopNo', shopNo);

    fileData.append(idx, files);

  

    // ajax

    $.ajax({

        url:'/intra_tmp/backend/image_upload.php',

        type:'POST',

        data:fileData,

        async:false,

        cache:false,

        contentType:false,

        processData:false

    }).done(function(response){

        alert(response);

    });

  

});



Articles

1 2 3 4 5 6 7 8 9