2012-08-02 28 views
5

Tôi nhận được biểu mẫu chỉ có một trường. Trường này thuộc loại 'managed_field'. Khi bạn nhấp vào nút "Tải lên", thanh tiến trình sẽ hiển thị cho bạn tiến trình tải tệp lên. Sau đó bạn sẽ cần gửi biểu mẫu để lưu tệp.Drupal 7 - tự động gửi biểu mẫu sau khi tải tệp lên bằng managed_file loại

Vì thanh tiến trình sẽ không hiển thị khi bạn chọn tệp và sau đó nhấp vào nút gửi biểu mẫu thay vì nút "Tải lên". Tôi muốn kích hoạt gửi biểu mẫu sau khi tải lên (thông qua nút "Tải lên") đã hoàn tất.

hình thức hiện tại của tôi trông như thế này:

$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'), 
); 

Module tập tin xử lý các tập tin thông qua một callback ajax đến tập tin/ajax/* uri. Gọi lại trả về lệnh ajax.

Về cơ bản tôi muốn thêm lệnh ajax bổ sung kích hoạt gửi biểu mẫu sau khi tải lên tệp đã hoàn tất.

+1

Điều đó có thể phức tạp. Một cách khác là tự động tải lên tệp khi lựa chọn để người dùng vẫn chỉ phải nhấp vào một nút. Xem http://drupal.stackexchange.com/questions/31121 – Clive

Trả lời

2

@Clive Đó không phải là một lựa chọn cho tôi vì tôi muốn người dùng bắt đầu tải lên. Bạn trả lời cho tôi một số ý tưởng mặc dù và tôi đã đưa ra giải pháp sau đây.

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(); 
        } 
       } 
      } 
     }); 

    } 
} 

Hy vọng điều này hữu ích cho những người dùng khác.

Thnx Clive để đặt tôi trên đường dẫn bên phải.

Các vấn đề liên quan