Perl CGI to download a file through a web browser

You will need a cgi-perl script that can download the file from the server machine. EX: click on the download link and it will open โ€œsave asโ€ and allow me to save the file on my local machine.

I created a web page using CGI, using this, I upload the file to the server and run the perl script to convert it to some other format (until I finish). Now I need to return this file (upload back) to the system.

#!/usr/bin/perl
#print "Content-type: text/html\n\n";
my $filepath='/upload/testing.pm';

    print "Content-Type: text/html\n\n";

    open("DOWNLOADFILE", "<".$filePath);
    while($fileContents = <DOWNLOADFILE>) {
        print $fileContents;
    }
print "Content-Type: text\n";
print "Content-Disposition: attachment; filename='testing.pm'\n";
print "Content-Description: File to download\n\n";
    close DOWNLOADFILE;

Upload the file from my computer (client) to the server computer, where I have a perl script that converts the file to a different format and saves the newly converted file to the ex directory: / upload-> until here I ended up with the script.

. test.pm .

+5
2

HTML. script.

use CGI;
my $html= new CGI;
#get the file name from URL ex. http://<server_IP>/cgi-bin/download.cgi?a=testing.txt
my $file= $html->param('a'); 
# $file is nothing but testing.txt
my $filepath= "/var/www/upload/$file";

print ("Content-Type:application/x-download\n");
print "Content-Disposition: attachment; filename=$file\n\n";

open FILE, "< $filepath" or die "can't open : $!";
binmode FILE;
local $/ = \10240;
while (<FILE>){
    print $_;
}

    close FILE;
 # use Unlink if u want to delete the files after download.
#unlink ($filepath);
+5

, . "", "".

( ):

, , , , CGI (Content-Type: text/html), , , CGI , . , . , .

, . , : -)

, . .

+3

All Articles