2012-07-11 51 views
1

交叉後http://perlmonks.org/?node_id=981067Perl的WWW ::機械化:: Firefox和輸入類型的文件

我有使用WWW ::機械化:: 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; 

執行時沒有錯誤,並且表單被submited但image0中沒有任何內容。

WWW的版本::機械化::火狐我用的是0.66我的Perl的版本是:對於MSWin32 86多線程構建v5.10.0

感謝

+0

在您當前的工作目錄中是否有文件「IMAGE.JPG」?也許文件名不是大寫? – simbabque 2012-07-11 15:19:02

回答

1

爲什麼不嘗試添加全路徑到圖像文件,並在field()方法東西鍵/值對等

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

此外,假設WWW::Mechanize::Firefox繼承所有的LWP::UserAgents方法,包括以下之前的代碼10

$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響應代碼,因此您會知道您正在收到正確的回覆。