Post/submit 使用 WWW::Mechanize 模块的 submit_form 例程的文件

Post/submit a file using submit_form routine of WWW::Mechanize module

我遵守准则

If you want to submit a file and get its content from a scalar rather than a file in the filesystem, you can use:

$mech->submit_form(with_fields => { logfile => [ [ undef, 'whatever', Content => $content ], 1 ] } );

来自 WWW::Mechanize documentation

我提交文件的代码

$mech->submit_form(with_fields => 
            { logfile => [ [ undef, "import_codes.xlsx", Content => $file_dir ], 1 ] });

它异常失败;

Can't call method "value" on an undefined value at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1568.
 at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1568.
    WWW::Mechanize::set_fields('WWW::Mechanize=HASH(0xf51b040)', 'logfile', 'ARRAY(0xf71e6dc)') called at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1948
    WWW::Mechanize::submit_form('WWW::Mechanize=HASH(0xf51b040)', 'form_name', 'inputform', 'fields', 'HASH(0xf71e920)') 

第 1560 行到第 1575 行 Mechanize.pm 的代码片段。

sub set_fields {
    my $self = shift;
    my %fields = @_;

    my $form = $self->current_form or $self->die( 'No form defined' );

    while ( my ( $field, $value ) = each %fields ) {
        if ( ref $value eq 'ARRAY' ) {
            $form->find_input( $field, undef,
                         $value->[1])->value($value->[0] );
        }
        else {
            $form->value($field => $value);
        }
    } # while
}

您似乎在表单中的文件输入字段中使用了错误的字段名称。 (强调我的)。

If you want to submit a file and get its content from a scalar rather than a file in the filesystem, you can use:

                                    vvvvvvv
$mech->submit_form(with_fields => { logfile => [ [ undef, 'whatever', Content => $content ], 1 ] } );

logfile 是你想要放置文件内容的输入字段的名称属性。在他们的示例中它是 logfile,但在你的真实您尝试提交的网站上的表单可能是其他内容。

$mech->submit_form calls $mech->form_with_fields。该方法的文档说:

Returns undef if no form is found.

当它执行 set_fields 时,它将失败,因为返回了 undef

使用正确的字段名称,应该可以。