WWW :: Mechanize提供损坏的上传文件名

时间:2012-07-06 16:47:01

标签: perl encoding www-mechanize

使用WWW::Mechanize上传带有西里尔文名称的文件时,我遇到了一些奇怪的问题。文件已正确上传但名称已损坏(我在目标网站上只看到??????)。

代码很简单:

use WWW::Mechanize;
use Encode qw(from_to);

my $config = {

    login         => "login",
    password      => "pass",
    source_folder => "$Bin/source_folder",
};

my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->agent_alias("Windows IE 6");

$mech->get("http://www.antiplagiat.ru/Cabinet/Cabinet.aspx?folderId=689935");
authorize($mech);

$mech->submit_form(

    form_number => 1,
    fields      => {},
    button =>
'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$FolderControl_StdFolder_0$DocumentsGrid$btnAddItem',
);

find( \&wanted, $config->{source_folder} );

sub wanted {

    return unless -f;

    say $config->{source_folder} . "/" . $_;

    #from_to($_, "CP1251", "UTF8"); doesn't work too :-(

    my $mech = $mech->clone();
    $mech->submit_form(

        form_number => 1,
        fields      => {

            'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$fuDocumentUpload' =>
              $config->{source_folder} . "/" . $_,
        },
        button => 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$btnCommitUpload',
    );
}

如果我将文件名从CP1251编码为UTF8,则上传不起作用。请帮我找一个解决方案。

1 个答案:

答案 0 :(得分:1)

以下是我使用的解决方案:

   my $filename = $_;
    from_to( $filename, "CP1251", "UTF8" );

    my $mech = $mech->clone();

    my $form = $mech->form_number(1);
    $mech->field( 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$fuDocumentUpload',
        $config->{source_folder} . "/" . $_ );
    $form->find_input(
        'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$fuDocumentUpload')->filename($filename);
    $mech->submit_form(

        form_number => 1,
        button => 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$btnCommitUpload',
    );