2010-12-02 72 views
1

我希望我能理解這個問題。jQuery上傳一個文件,然後通常提交表格

我目前正在使用jQuery Form Plugin + Validation,它非常棒。

但是我想做到的是,

1,上傳的臨時文件在下一步

2操縱,一旦文件已被上傳,提交表單通常去下一步(新文件)。

所以我只是100%確定如何做到這一點,我目前使用此代碼。

$("#fileUpload").validate({ 
     submitHandler: function(form) { 

      $(form).ajaxSubmit({ 
       beforeSubmit: function() { 
        $('input').attr("disabled", true); 
        $("#uploadResponse").show('slow'); 
        $('#uploadResponse').append("<img src='images/icons/ajax-loader.gif' />"); 
       }, 
       success: function(responseText, statusText, xhr, form) { 
        $('input[type=submit]').attr("disabled", false); 
        $('input[type=file]').attr("disabled", true); 
       }, 
       error: function (responseText, statusText, xhr, form) { 
        alert("Oops... Looks like there has been a problem."); 
       } 
      }); 
     } 
    }); 

而對於我的形式

<div id="uploadResponse" style="display: none;"> File Uploading, this can take a few minutes... </div> 

<form action='page.ImportContacts2.php' name="mergeCSV" id="fileUpload" method="post" enctype="multipart/form-data" > 
     File: <input id="getCSV" name="getCSV" class="required" type="file"> 

     Delimiter Type: 
     <select id="delimiter" name="delimiter"> 
      <option value="1" selected="selected">Comma</option> 
      <option value="2">Tab</option> 
     </select> 

     Enclosure Type: 
     <select id="enclosure" name="enclosure"> 
      <option value="1" selected="selected">Double Quotes (")</option> 
      <option value="2">Single Quotes (\')</option> 
     </select> 

     <button type="submit" name="SubmitCSV" id="SubmitCSV" value="SubmitCSV" class="csvCol3Button buttonSmall">Upload File</button> 

</form> 

回答

1

答案很簡單:你無法通過AJAX上傳文件。您必須將表單提交到另一個頁面,然後從那裏開始。

更多:技術上你可以,但不是這樣。它涉及到創建一個iframe並通過它提交文件。無論哪種方式,你必須有一個回帖,你不能使用XMLHttpRequest對象。

如果你做一個quick google search,你可以找到爲你做這個的插件。

+0

嘿,目前我使用這個表單提交: – Justin 2010-12-02 01:30:22