2009-07-09 32 views
0

我正在嘗試實現文件上傳的進度指示器。腳本的Part1和Part2如果單獨執行,則可以正常運行。但是,如果一起執行,腳本停止在:爲什麼我的Perl腳本在從Windows上的stdin讀取後使用CGI模塊時會停止?

my $cg = new CGI(); 

該問題只發生在Windows服務器上。可能是什麼原因?

 
#!C:\Perl\bin\perl.exe -w 
use CGI; 
$post_data_filename = "C:\\test\\postdata.txt"; 
$uploaded_filename = "C:\\test\\uploaded_file.txt"; 

#PART 1 
# read and store the raw post data in a temporary file so that we can repeatedly 
# look at size of this temporary file in order to implement a progress bar 
open(TMP,">","$post_data_filename"); 
$len = $ENV{'CONTENT_LENGTH'}; 
read (STDIN ,$LINE, $len); 
print TMP $LINE; 
close (TMP); 

#PART 2 
#use a CGI instance to read the raw post data and extract the uploaded file from it 
my $cg = new CGI(); 
open(STDIN,"$post_data_filename"); 
my $fh = $cg->upload('file[0]'); 
open($tmp_fh, ">$uploaded_filename"); 
while(<$fh>) { 
    print $tmp_fh $_; 
    } 
close($tmp_fh); 

print "Content-type: text/html\n\n"; 
print "Ready\n"; 

exit; 
+0

請`使用strict`。避免使用間接對象符號:my $ cgi = CGI-> new更可取。我不確定你在做什麼:所有這些代碼都在同一個文件中?您的腳本是否有權寫入`C:\ test`? – 2009-07-09 13:05:55

回答

2

嘗試在從它讀取之前做binmode(STDIN)。我猜你會以比內容長度說的更少的字節結束,這會導致CGI陷入困境。您可能還需要在重新打開STDIN後執行binmode。

另外,請檢查您的所有IO操作是否成功。

1

在Windows上,文件無法打開進行讀取,而另一個進程已打開進行寫入。

而你的上傳米將不起作用,因爲你讀了整個STDIN,然後把它寫到TMP,所以你從0%直接到100%。

相關問題