Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

WWW::Mechanize uploaded file name problem

by Gangabass (Priest)
on Jul 06, 2012 at 16:41 UTC ( #980320=perlquestion: print w/ replies, xml ) Need Help??
Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks

I have some weird problem while uploading file with Cyrillic name via WWW::Mechanize. The file is uploaded correctly but the name is broken (I see only ?????? on the target site).

The code is simple:

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=68 +9935"); authorize($mech); $mech->submit_form( form_number => 1, fields => {}, button => 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$FolderControl_StdFolder_0$D +ocumentsGrid$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$fuDocumentUploa +d' => $config->{source_folder} . "/" . $_, }, button => 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$btnCommit +Upload', ); }

If I encode file name from CP1251 to UTF8 than upload doesn't work... Please help me to find solution.

Update:

Here is solution I use (File upload w/WWW::Mechanize)

my $filename = $_; from_to( $filename, "CP1251", "UTF8" ); my $mech = $mech->clone(); my $form = $mech->form_number(1); $mech->field( 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$fuDocumen +tUpload', $config->{source_folder} . "/" . $_ ); $form->find_input( 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$fuDocumentUpload')- +>filename($filename); $mech->submit_form( form_number => 1, button => 'ctl00$ctl00$Body$MainWorkSpacePlaceHolder$btnCommit +Upload', );

Comment on WWW::Mechanize uploaded file name problem
Select or Download Code
Re: WWW::Mechanize uploaded file name problem
by Anonymous Monk on Jul 06, 2012 at 16:52 UTC

    The file is uploaded correctly but the name is broken (I see only ?????? on the target site).

    The problem is entirely on the target site, nothing for you to do, unless you want to send some ASCII filename

    See also Unable to get filename with quotes after upload?

      But why it work from the browser?
Re: WWW::Mechanize uploaded file name problem
by Anonymous Monk on Jul 06, 2012 at 20:26 UTC
Re: WWW::Mechanize uploaded file name problem
by zwon (Monsignor) on Jul 07, 2012 at 10:30 UTC
    Note that from_to($_, "CP1251", "UTF8") doesn't turn UTF-8 flag on. Try $_=decode("CP1251", $_) instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://980320]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-08-30 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls