我正在尝试实施文件上传的进度指示器。如果单独执行,脚本的Part1和Part2可以正常运行。但如果一起执行,脚本会停在:
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 :(得分:2)
在阅读之前尝试做binmode(STDIN)。我猜你最终会得到比内容长度更少的字节,这会导致CGI陷入困境。重新打开STDIN后,您可能还需要执行binmode。
另外,请检查所有IO操作是否成功。
答案 1 :(得分:1)
在Windows上,无法打开文件进行阅读,而另一个进程已打开进行写入。
并且您的上传计量器将无法工作,因为您阅读了整个STDIN然后将其写入TMP,因此您从0%直接变为100%。