下载cgi网页的url内容

时间:2012-09-17 20:36:38

标签: perl cgi

我有本地平面数据库搜索的cgi搜索引擎,我想添加用户能够导出/下载搜索结果的选项。用cgi可能吗? 这是代码。

     #!/usr/bin/perl
      read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
      # Split the name-value pairs
      @pairs = split(/&/, $buffer);
      foreach $pair (@pairs) {
       ($key, $value) = split(/=/, $pair);

     $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

     $formdata{$key}.= "$value";
      }
   $search = $formdata{'search'};
   open(INFO, "/test.txt");   
   @array=<INFO>;
   close (INFO);
   print "Content-type:text/html\n\n";
   print "<html>\n";
    print "<head><title>Search result</title></head>\n";
   print "<body>\n"; 
   print "<h4><font color=#990000>This is your search result!</h4>\n"; 
   $search_url = 'https://test.php';
   foreach $line (@array) {
   if ($line =~ /$search/){
   ($host,$ip)=split(/\|/,$line);
   $records= ++$counter;
   @result =($host,$ip);
   print "<font color=#7a378b><b><p>";
   print "<table border=0 cellpadding=0 cellspacing=0 style=border-collapse: collapse       
   bordercolor=#111111 width=20% bgcolor=#C0C0C0>";

   print "</tr>";
   foreach (@result) {
   @words = split ;
   print     "<tr><td width=33% bgcolor=#DCDCDC><b><font color=#000080 size=1   face=Courier New>$words[0]</font></b></td>";
  print     "<td width=36% bgcolor=#DCDCDC><b><font color=#000080 size=1 face=Courier    
  New>$words[1]</font></b></td>";
  print "</table>";
 }
 }
   }

  if ($records== 0) {
  print " Sorry! No records found\n";
   }

1 个答案:

答案 0 :(得分:3)

是。传统方式是输出适当的内容类型标题,例如

Content-type: text/csv
Content-type: text/xml

并可选择指定“内容处置”。在符合浏览器的情况下,这将为客户端提供将服务器输出保存到本地文件的选项:

Content-disposition: attachment;filename="myfilename.csv"

(编辑以使用friedo明智的建议)