2010-08-10 38 views
4

Firefox 3.6引入了[常規類型=「文件」輸入元素的多重屬性]( http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/)。Perl的CGI.pm能處理Firefox的<input type =「file」,multiple =「」>表單字段嗎?

我無法讓Perl來處理這些字段。我可以在這樣的列表環境中調用該字段:

my @files = $CGIobject->param("File_Input"); 

通過循環,將給我的文件名稱作爲字符串,但沒有別的。

任何建議將非常歡迎。

這裏的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 

    <head> 
    <title>Multiple file upload test</title> 

    </head> 

    <body> 

    <form action="deliberately_obfuscated" 
      method="post" 
     enctype="multipart/form-data"> 

     <input type="file" 
      name="multiple_files" 
     multiple="true"/> 

     <button type="submit">Submit</button> 

    </form> 

    </body> 

</html> 

這裏是Perl:

#!/usr/bin/perl 

#use strict; 
#use warnings; 

use CGI; 

my $CGIo = new CGI; 

print $CGIo->header(); 

@lightweight_fh = $CGIo->upload('myfiles'); 

# undef may be returned 
# if it's not a 
# valid file handle 

if (@lightweight_fh) { 

    # Upgrade the handle to 
    # one compatible with IO::Handle: 

    my $io_handle = $lightweight_fh->handle; 

    open (OUTFILE,'>>','/hidden_deliberately/'); 

    while ($bytesread = $io_handle->read($buffer,1024)){ 

    print OUTFILE $buffer; 

    } 

} 

腳本不會進入

if (@lightweight_fh) { 

塊。

我試過數據:在iflight塊之前的@lightweight_fh上的Dumper,它從字面上完全沒有打印任何東西。

+0

多文件輸入將爲您提供一個字段數組,其中只有一個字段。我不會說Perl,所以我不知道如何改變語法,但它應該非常簡單。 – 2010-08-10 10:51:20

+0

始終發佈完整的示例腳本,以便我們可以看到您所做的一切。 – 2010-08-10 12:16:00

回答

5

嗚呼,得到了這個工作。大手剎問題?舊的CGI.pm版本!很遺憾,CGI.pm文檔不包含諸如「版本X中引入的」等功能的註釋。許多其他模塊/庫/軟件包。

碰巧我的版本是3.15,當前是3.49。我甚至在嚴格模式下工作。有人知道斯坦因爲什麼使用非嚴格的例子嗎?

這裏的XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 

    <head> 
    <title>Multiple file upload test</title> 

    </head> 

    <body> 

    <form action="deliberately_hidden" 
      method="post" 
     enctype="multipart/form-data"> 

     <input type="file" 
      name="multiple_files" 
     multiple="true"/> 

     <button type="submit">Submit</button> 

    </form> 

    </body> 

</html> 

這裏的Perl的:

#!/usr/bin/perl 

    use strict; 
    use warnings; 

    use CGI; 

    my $CGIo = new CGI; 

    print $CGIo->header(); 

    my @lightweight_fh = $CGIo->upload('multiple_files'); 

    foreach my $fh (@lightweight_fh) { 

    # undef may be returned if it's not a valid file handle 

    if (defined $fh) { 

     # Upgrade the handle to one compatible with IO::Handle: 

     my $io_handle = $fh->handle; 

     open (OUTFILE,'>>','/deliberately_hidden/' . $fh); 

     while (my $bytesread = $io_handle->read(my $buffer,1024)) { 

     print OUTFILE $buffer 

     } 

    } 

    } 

感謝您的幫助大家。

6

CGI.pm中使用upload方法。

在列表上下文中,upload()將返回一個文件句柄數組。這使得可以處理多個上傳字段使用相同名稱的表單。

+1

嚴格來說,表單並未發送「多個上傳字段」。表單正在發送具有多個值的單獨上傳字段。 因此,CGI.pm文檔的摘錄是否適用? – 2010-08-11 06:56:19

3

它看起來並不像你在使用CGI.pm跟蹤Processing a file upload field的文檔。在我們深入研究這個問題之前,你可以使用文檔化的方法來完成一個文件嗎?

+0

我最初並未遵循記錄的方法,但正如您所看到的,我已經嘗試了上述編輯過的示例,但該方法似乎也不奏效。 對不起,這個例子沒有很好的格式,但我是新來張貼在這個網站上。 – 2010-08-11 06:50:26

1

是,Perl的CGI.pm可以proess Firefox的多文件上傳

想看?使用此快捷方式:

use Data::Dumper; 
print '<pre>', $CGIo->escapeHTML(Dumper($CGIo)),'</pre>'; 

你會看到類似這樣的:

$VAR1 = bless({ 
     '.parameters' => [ 
          'filename', 
          'submit' 
          ], 
     'use_tempfile' => 1, 
     '.tmpfiles' => { 
          '*Fh::fh00003temp-2.txt' => { 
                 'info' => { 
                    'Content-Type' => 'text/plain', 
                    'Content-Disposition' => 'form-data; name="filename"; filename="temp-2.txt"' 
                    }, 
                 'name' => bless(do{\(my $o = 'C:\\WINDOWS\\TEMP\\CGItemp52869')}, 'CGITempFile'), 
                 'hndl' => bless(\*{'Fh::fh00003temp-2.txt'}, 'Fh') 
                 }, 
          '*Fh::fh00001temp-1.txt' => { 
                 'info' => { 
                    'Content-Type' => 'text/plain', 
                    'Content-Disposition' => 'form-data; name="filename"; filename="temp-1.txt"' 
                    }, 
                 'name' => bless(do{\(my $o = 'C:\\WINDOWS\\TEMP\\CGItemp52775')}, 'CGITempFile'), 
                 'hndl' => bless(\*{'Fh::fh00001temp-1.txt'}, 'Fh') 
                 } 
         }, 
     '.charset' => 'ISO-8859-1', 
     'param' => { 
         'filename' => [ 
             $VAR1->{'.tmpfiles'}{'*Fh::fh00001temp-1.txt'}{'hndl'}, 
             $VAR1->{'.tmpfiles'}{'*Fh::fh00003temp-2.txt'}{'hndl'} 
            ], 
         'submit' => [ 
            'Process File' 
            ] 
        }, 
     'escape' => 1, 
     '.header_printed' => 1 
     }, 'CGI'); 

首先你打電話給你的文件上傳現場File_Input,那麼你怎麼稱呼它multiple_files,那麼你怎麼稱呼它MYFILES - 你必須使用相同的名字,這很重要。

此外,$ lightweight_fh和@lightweight_fh是兩個不同的變量,你需要

for my $lightweight_fh ($CGIo->upload('multiple_files')){ 
    my $io_handle = $lightweight_fh->handle; 
    ... 
} 

此外,您嘗試打開一個目錄「/ hidden_​​deliberately /」作爲一個文件,你不檢查對於錯誤

+0

Hi Tago。我保證字段的命名不適用於我的測試。我的答案始終使用「multiple_files」。不同的命名只是一個錯誤。 與/ hidden_​​directory /相同。只是試圖混淆隱私原因的道路。我確實有「或者死亡$!」在公開招募的結尾,但由於堆棧溢出的愚蠢的窄列布局而被切斷。 Re @lightwight_fh vs $ lightweight_fh我知道他們是不同的變數,但是我對上下文感到困惑,並且一度在努力獲得使用doc'd方法上傳的多字段的第一個文件。 – 2010-08-14 07:44:57

相關問題