2014-01-20 1 views
1

blueimp의 파일 업로드 사용 Ajax 요청 성공 후 파일 업로드를 어떻게 트리거합니까? fileuploadsubmit을 실행하려고 시도했지만 데이터가 정의되지 않았다고 표시됩니다.Blueimp 성공적인 Ajax 요청 후 Fileupload 트리거 제출 버튼

HTML :

<form id="add-project-form" method="post" action="http://localhost/project/add/"> 
    <p> 
     <label for="project">Project Name:</label> 
     <input id="project" class="input-text" type="text" name="name" /> 
    </p> 
    <p> 
     <label for="overview">Project Overview:</label> 
     <input id="overview" class="input-text" type="text" name="overview" /> 
    </p> 
    <p><input type="submit" value="Add Project" /> 
</form> 

<form id="logo-upload" method="post" action="http://localhost/project/upload/" enctype="multipart/form-data"> 
    <p> 
     <input type="file" name="logo" id="logo" /> 
    </p> 
</form> 

jQuery를 :

$('#logo-upload').fileupload({ 
    dataType: 'json', 
    maxNumberOfFiles: 1, 
    autoUpload: false 
}).on('fileuploadsubmit', function(e, data) { 
    console.log(data); 
}); 


$(document).on("submit", "#add-project-form", function(e) { 
    e.preventDefault(); 

    var data = $(this).serialize(); 
    var url  = $(this).attr("action"); 

    $.ajax({ 
     url: url, 
     data: data, 
     type: "post", 
     dataType: "json", 
     success: function(response) { 
      if(response && response.location != undefined) { 

       // I would want the upload to begin here 

      $('#logo-upload').fileupload().trigger('fileuploadsubmit'); 
      } 
     } 
    }); 
}); 
+0

'e.data'에 무엇이 들어 있습니까? 어디에서'.on ('fileuploadsubmit', function (e, data) {'from?) 두 번째 매개 변수를 포함해야합니까? –

+0

아니, 그것은 나를 위해 작동 http://jsfiddle.net/eLLWN/29/ –

+0

@KevinB 다음은'e' 객체의 스냅 샷입니다 : http://screencast.com/t/rINDI0uO'.on ('fileuploadsubmit', function (e, data) {'나는 이것을 따라 갔다 : http : //stackoverflow.com/questions/19807361/uploading-multiple-files-asynchronously-by-blueimp-jquery-file 업로드 – gwinh

답변

4

바인드 버튼에 기능을 제출하고 Ajax 요청 후 클릭을 강제로 : 다음은 초기 코드

$('#logo-upload').fileupload({ 
    dataType: 'json', 
    maxNumberOfFiles: 1, 
    autoUpload: false, 
    add : function(e,data){ 
      $("#uploadButton").on("click",function(){ 
       data.submit(); 
      }) 
      } 
}).on('fileuploadsubmit', function(e, data) { 
    console.log(data); 
}); 

$.ajax({ 
    url: url, 
    data: data, 
    type: "post", 
    dataType: "json", 
    success: function(response) { 
     if(response && response.location != undefined) { 
      $("#uploadButton").click(); 
     } 
    } 
}); 
+0

이벤트에 대한 방아쇠 역할을하는 새로운 요소를 도입 할 생각은별로 없지만 문제는 해결됩니다. – gwinh

+0

'add' 이벤트에 대한 ajax 요청을 만들고'data.submit'를 성공적으로 호출하십시오. –

+0

훌륭한 일을했습니다. 나를 위해. 감사! – Olokoo