以多步形式Drupal 7获取文件数据

时间:2014-09-22 10:19:29

标签: file upload drupal-7

我正在尝试以多步骤形式上传和解析文件:

multistep_example_menu:

function multistep_example_menu() {
  $items = array();
  $items['admin/game/xml'] = array(
        'title' => 'Upload Page Note [XML]',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('multistep_example_form'),
        'access callback' => TRUE
  );
  return $items;
}

multistep_example_form:

    function multistep_example_form($form, &$form_state) {
        $form['#prefix'] = '<div id="multistep-example-form-wrapper">';
        $form['#suffix'] = '</div>';

        $form['#tree'] = TRUE;
        $step = empty($form_state['storage']['step']) ? 1 : $form_state['storage']['step'];
        $form_state['storage']['step'] = $step;

    switch ($step) {
        case 1:
            $form['step1'] = array(
                '#type' => 'fieldset',
                '#title' => '1. Upload your XML',
            );

            $form['step1']['uploadxml'] = array(
                '#type' => 'file',
                '#title' => 'Select your XML',
                '#upload_location' => 'public://files'
            );
            break;

        case 2:
            // I need to get data from xml file and display them
            break;
    }

$form['actions'] = array('#type' => 'actions');
if ($step == 2) {
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => 'Display',
        );
    }
if ($step < 2) {
        $form['actions']['next'] = array(
            '#type' => 'submit',
            '#value' => 'Next step',
            '#ajax' => array(
                'wrapper' => 'multistep-example-form-wrapper',
                'callback' => 'multistep_example_ajax_callback',
            ),
        );
    }
if ($step > 1) {
        $form['actions']['prev'] = array(
            '#type' => 'submit',
            '#value' => 'Prev step',
            '#limit_validation_errors' => array(),
            '#submit' => array('multistep_example_form_submit'),
            '#ajax' => array(
                'wrapper' => 'multistep-example-form-wrapper',
                'callback' => 'multistep_example_ajax_callback',
            ),
        );
    }
return $form;

multistep_example_ajax_callback:

function multistep_example_ajax_callback($form, $form_state) {
    return $form;
}

multistep_example_form_submit:

function multistep_example_form_submit($form, &$form_state) {

    $validators = array('file_validate_extensions' => array('xml'));
    $file = file_save_upload('uploadxml', $validators, 'public://', FILE_EXIST_REPLASE);
    if($file) {
     var_dump($file);
    }
}

var_dump返回NULL。我做错了什么?

1 个答案:

答案 0 :(得分:0)

file_save_upload函数通过表单元素中的name属性选择文件。更具体地说,是数组格式。如果您为文件元素指定了'#name' => 'files[xml]',这样的名称,那么您可以使用file_save_upload('xml')

获取该文件