简体   繁体   中英

Perl CGI download file

I need to create an HTML file which input text or file. The text will be processed and printed while the file is processed and downloaded.

I am not able to create download. I tried with download header, but it didn't work. A new file will be created with @array after processing the file. output_file.txt should be downloadable by the user.

#!C:/perl64/bin/perl.exe
use strict;
use warnings;
use CGI::Pretty qw(:all);


# HTML
print header();
print start_html();
print start_form();
print textfield('text');
print (submit);
print filefield('file');
print submit();
print end_form();
print end_html();

if (param ('file')) {
    my $fh = param('file');
# File processed to get @result and made new file
    open (OUT, ">output/output_file.txt");
    print OUT @result;
# Need to download output_file.txt file
}

# Text processed and printed
elsif(param('text')){
    my $text = param('text')
}

Try the following:

#!/usr/bin/perl -w
use strict;
use warnings;
use CGI::Pretty qw(:all);
my @result;
if (param ('file')) {
    my $file_name=param('file');
    my $file_handle =upload('file');
    my $file_size=-s $file_handle;
    print header(
        -type=>'application/octet-stream',         
        -attachment=> $file_name,
        -Content_Length=>$file_size
    );
# file processed to get @result and made new file 
    binmode($file_handle);
    while (<$file_handle>){
        print $_;
        push (@result,$_);
    }
# need to download output_file.txt file
}
# text processed and printed
elsif(param('text')){
    my $text = param('text');
    print header(
        -type=>'application/octet-stream',         
        -attachment=> "Sample.txt",
        -Content_Length=>length($text)
    );
    print $text;
}

# html 
print header();
print start_html();
print start_form();
print textfield('text');
print (submit);
print end_form();
print start_form();
print filefield('file');
print submit();
print end_form();
print end_html();

Here's an example I modified:

#!C:\Program Files\perl\bin\perl.exe

use CGI ':standard';  
use CGI::Carp qw ( fatalsToBrowser );  

$input_val = $ENV{'QUERY_STRING'};

($field_name, $command) = split (/=/, $input_val);
($file_name, $option_name) = split(/&/, $command);


$file_path= "Database/$file_name";

$directorypath = "Database/";

my $files_location;  
my @fileholder;  

$files_location = $directorypath;  

if ($file_name eq '')
{  
    print "Content-type: text/html\n\n";  
    print "File doesn't exist";  
} else {  

    open(DLFILE, "<$files_location/$file_name") || Error('open', 'file');  
    @fileholder = <DLFILE>;  
    close (DLFILE) || Error ('close', 'file');  

    print "Content-Type:application/x-download\n";  
    print "Content-Disposition:attachment;filename=$file_name\n\n";  
    print @fileholder;  
}

print "</HTML>";
exit 0;

Refer to this website: http://www.perlnotes.com/study0680.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM