2012-09-17 86 views
1

我有用於本地平面數據庫搜索的cgi搜索引擎,我想添加選項用戶能夠導出/下載搜索結果。這可能與CGI? 這是代碼。下載cgi網頁的url內容

 #!/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"; 
    } 
+0

你將不得不增加更多的細節 – andrewsi

+0

我已經添加了我的代碼 – eli1128

回答

3

是的。傳統的方式是輸出適當的內容類型頭,例如,

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

並且可選地指定「內容處置」。在符合瀏覽器,這會給客戶端到服務器的輸出保存到本地文件的選項:

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

(編輯使用friedo的英明建議)

+1

如果您打算使用Content-Disposition標頭,我建議您始終引用文件名部分,即'filename =「myfilename.csv」'。這樣可以避免當你需要文件名和空白時出現錯誤。 – friedo

+0

謝謝,所以應該在我的代碼的頂部或底部添加「Content-disposition」?或需要更多的東西 – eli1128

+1

這是一個標題,所以它應該在任何內容之前輸出。 – mob