簡體   English   中英

如果在Windows上從stdin讀取后使用了CGI模塊,為什么我的Perl腳本會停止?

[英]Why does my Perl script halt if CGI module is used after reading from stdin on Windows?

我正在嘗試為文件上傳實現進度指示器。 如果分別執行,腳本的第1部分和第2部分將正常運行。 但是如果一起執行,腳本將在以下位置停止:

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;

嘗試從中讀取binmode(STDIN)。 我猜您最終得到的字節數少於內容長度所說的字節數,這導致CGI混亂。 重新打開STDIN之后,您可能還需要執行binmode。

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

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

而且您的上傳計量器將無法工作,因為您先讀取了整個STDIN,然后將其寫入TMP,所以您從0%直接變為100%。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM