2012-08-02 36 views
5

我得到一個只有一個字段的表單。該字段的類型爲'managed_field'。當您點擊「上傳」按鈕時,進度條會顯示文件上傳的進度。之後,您需要提交表單以保存文件。Drupal 7 - 文件上傳後自動提交表單與managed_file類型

由於當您選擇文件時,進度條不會顯示,然後單擊表單提交按鈕而不是「上傳」按鈕。我想在上傳後(通過「上傳」按鈕)完成後觸發表單提交。

我目前的形式是這樣的:

$form['#attributes'] = array('enctype' => "multipart/form-data"); 

$form['pdf_upload'] = array(
    '#title' => t('Upload PDF'), 
    '#type' => 'managed_file', 
    '#required' => TRUE, 
    '#progress_message' => t('Please wait...'), 
    '#progress_indicator' => 'bar', 
    '#upload_validators' => array(
     'file_validate_extensions' => array('pdf'), 
    ) 

); 

$form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Save'), 
); 

文件模塊通過Ajax回調到文件/ AJAX/* URI處理文件。回調函數返回ajax命令。

基本上我想添加額外的ajax命令,在文件上傳完成後觸發表單提交。

+1

這可能會很棘手。另一種方法是自動上傳選擇的文件,這樣用戶仍然只需點擊一個按鈕。請參閱http://drupal.stackexchange.com/questions/31121 – Clive 2012-08-02 12:44:48

回答

2

@Clive這不是我的選擇,因爲我希望用戶自己開始上傳。你的回答給了我一些想法,但我提出了以下解決方案。

Drupal.behaviors.fileUpload = { 
    attach: function(context, settings) { 
     jQuery("body").ajaxComplete(function(event,request, settings){ 
      // Only do something when on the orders page of a user 
      // This is where I use the upload functionality 
      if(window.location.pathname.match(/user\/\d+\/orders/)) { 
       // Check if the AjaxComplete was triggered by the managed file upload 
       // pdf_upload_XXX is my form name 
       // Get the form-build-id from the URL 
       if (form_build_id = settings.url.match(/file\/ajax\/pdf_upload_\d*\/(.*)$/)) { 
        // Check if the upload has completed by checking if there is a Delete button in the form that has the form-build-id 
        if(jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id$=remove-button]').length) { 
         // Click the submit button 
         jQuery('[value="'+form_build_id[1]+'"]').closest('form').find('[id^=edit-submit]').click(); 
        } 
       } 
      } 
     }); 

    } 
} 

希望這對其他用戶也有用。

Thnx Clive讓我在正確的道路上。

相關問題