Drupal 7 - automatically submit a form after uploading a file using the managed_file type

I received a form with only one field. This field is of type "managed_field". When you click the "Download" button, a progress bar will show you the file download progress. After that, you will need to submit a form to save the file.

Since the progress bar will not be displayed when you select the file, then click the "Submit Form" button instead of the "Download" button. I would like to start submitting the form after the download is completed (via the "Download" button).

My current form is as follows:

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

The file module processes files through an ajax callback for the file / ajax / * uri. The callback returns ajax commands.

ajax, .

+5
1

@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 .

+2

All Articles