【问题标题】:Perl WWW::Mechanize::Firefox and input type filePerl WWW::Mechanize::Firefox 和输入类型文件
【发布时间】:2012-07-11 10:43:40
【问题描述】:

跨帖http://perlmonks.org/?node_id=981067

我在使用 WWW::Mechanize::Firefox 将文件上传到带有使用输入类型文件的表单的站点时遇到问题,如下所示:

<form enctype="multipart/form-data" action="uploader.php" method="POST" id="formular">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="image0" type="file" id="image0"/><br />
<input type="submit" value="Upload File" />

uploader.php的内容如下:

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['image0']['name']); 

if(move_uploaded_file($_FILES['image0']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['image0']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file,".  basename( $_FILES['image0']['name'])." please try again!";
}
?>

我用于上传文件的代码如下:

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize::Firefox;

my $bot = WWW::Mechanize::Firefox->new( autoclose => 0,activate =>1);

$bot->get('http://127.0.0.1/file/index.html');
$bot->form_id('formular');
$bot->field('image0','IMAGE.JPG');
$bot->submit;

执行没有错误,提交了表单但image0中没有任何内容。

我使用的 WWW::Mechanize::Firefox 版本是 0.66 我的 perl 版本是:v5.10.0 为 MSWin32-x86-multi-thread 构建

谢谢

【问题讨论】:

  • 您当前的工作目录中是否有文件IMAGE.JPG?也许文件名不是大写的?

标签: perl mechanize www-mechanize


【解决方案1】:

为什么不尝试在field() 方法中添加图像文件的完整路径和键/值对,例如

my $image_path = "/home/images/IMAGE.JPG";
$bot->field(image0=>$image_path);
$bot->submit();

另外,假设WWW::Mechanize::Firefox 继承了所有LWP::UserAgents 方法,请在$bot-&gt;submit(); 之前包含以下代码

$bot->add_handler("request_send",  sub { shift->dump; return });
$bot->add_handler("response_done", sub { shift->dump; return });

这将启用登录您的代码。注意日志文件中的请求和响应代码,例如“HTTP 200 OK”或“HTTP 302 Found”。这些是标准 HTTP 响应代码,因此您会知道您得到了正确的响应。

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2011-12-07
    • 2016-04-12
    相关资源
    最近更新 更多