Drupal7-文件上传后自动提交表单,文件类型为managed_file



我得到了一个只有一个字段的表单。此字段的类型为"managed_field"。当您单击"上传"按钮时,进度条将显示文件上传的进度。之后,您需要提交表格以保存文件。

因为当你选择一个文件,然后点击表单提交按钮而不是"上传"按钮时,进度条不会显示。我想在上传(通过"上传"按钮)完成后触发表单提交。

我当前的表单如下:

$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/*uri的ajax回调来处理文件。回调返回ajax命令。

基本上,我想添加一个额外的ajax命令,在文件上传完成后触发表单提交。

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

希望这对其他用户也有用。

感谢克莱夫让我走上了正确的道路。

最新更新