in reply to Need to use data passed from FORM from HTML page to CGI upload script.

Hi

Remember Re: Need to print file contents on page (cgi101)?

Why all the "()" in "sub name() {..." ?

I fix for you

#!/usr/bin/perl -- ## ## ## ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while for " +-otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" ## #!/usr/bin/perl -- use CGI::Carp qw( fatalsToBrowser ); use strict; use warnings; use CGI (); main( @ARGV ); exit( 0 ); sub main { # return DebugCGI(); $CGI::POST_MAX = 1024 * 5000; my $upload_dir = 'goner'; my $query = CGI->new; my( $headers, $body ) = SaveUploadsTo( $query, $upload_dir ); print $headers, $body; } ## end sub main sub SaveUploadsTo { my( $query, $upload_dir ) = @_; return $query->header, PrintPage( $query ) if not $query->first_param( "filecsv" ); my $filename = WashFilename( $query->first_param( "filecsv" ) ); ## imaginary alternative # my( $filename, $error ) = WashFilename( $query->first_param( "fil +ecsv" ) ); # $error and return $query->header, ErrorPage( $query, $error ); my $tmpfilename = $query->tmpFileName( $query->first_param( "filec +sv" ) ); $tmpfilename or return $query->header, ErrorPage( $query, "No file uploaded" +); $filename = "$upload_dir/$filename"; require File::Copy; File::Copy::copy( $tmpfilename, $filename ) or die "Copy to ( $filename ) failed: (( $! ))(( $^E ))"; return $query->header, ThanksPage( $query ); } ## end sub SaveUploadsTo sub ErrorPage { my( $query, $message ) = @_; return qq{<!DOCTYPE html> <html lang="en"> <title> Error </title> <h1> $message </h1> } } ## end sub ErrorPage sub CGI::first_param { return my( $first ) = CGI::multi_param( @_ ); } sub DebugCGI { my( $cgi ) = @_; $cgi ||= CGI->new; binmode STDOUT, ':encoding(UTF-8)'; $cgi->charset( 'UTF-8' ); print $cgi->header( -charset => 'UTF-8' ); print $cgi->start_html, $cgi->b( rand time, ' ', scalar gmtime ), '<table border="1" width="%100"><tr><td>', $cgi->Dump, '</td>', '<td><div style="white-space: pre-wrap; overflow: scroll;">', $cgi->escapeHTML( DD( $cgi ) ), '</div></td></tr></table>', CGI->new( \%ENV )->Dump, $cgi->end_html; } ## end sub DebugCGI sub DD { require Data::Dumper; return scalar Data::Dumper->new( \@_ )->Indent( 1 )->Useqq( 1 )->D +ump; } sub WashFilename { use File::Basename; my $basename = basename( shift ); # untainted , only use a-z A-Z 0-9 and dot and dash $basename = join '', $basename =~ m/([\-.a-zA-Z0-9])/g; # basename is now, hop0efully, file.ext ## so to ensure uniqueness, we adulterate it :) my $id = $$ . '-' . time; my( $file, $ext ) = split /\./, $basename, 2; return join '.', grep defined, $file, $id, $ext; } ## end sub WashFilename sub ThanksPage { my( $query ) = @_; q{ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-s +trict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Thanks!</title> <style type="text/css"> img {border: none;} </style> </head> <body> <p>Thanks for uploading your file!</p> </body> </html> } } ## end sub ThanksPage sub MaintenancePage { q{<!DOCTYPE html> <html> <head> <title>MAINTENANCE PAGE</title> </head> <body> <form name = f1 action="https://10.XXX.XXX.X:16311/ibm/console/webtop/ +cgi-bin/download_csv.cgi" method = "POST"> <p style="margin-left:10em;font-size:40px">MAINTENANCE PAGE</p> <br> <p style="margin-left:16.5em;font-size:20px">Circle:<select name="Circ +le" > <option value="Gabon">Gabon</option> <option value="Tanzania">Tanzania</option> </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs +p;&nbsp;&nbsp; Technology : <select name="Techno" > <option value="Core">Core</option> <option value="RAN">RAN</option> </select> </p> <br> <input type="submit" style="margin-left:30.5em" value="Download" onc +lick="this.form.target='_blank';return true;"> &nbsp;&nbsp;&nbsp; <input type="submit" value="Upload" onclick="f1.action='https://10.XXX +.XXX.X:16311/ibm/console/webtop/cgi-bin/Maintenance_Framework.cgi'; +return true;"> </form> </body> </html> }; } ## end sub MaintenancePage sub PrintPage { my( $q ) = @_; <<'__HTML__'; <!DOCTYPE html> <form name=f1 style="margin:20px 0" action="Maintenance_Framew +ork.cgi" method="post" enctype="multipart/form-data"> <p> <h3 align='center'> Maintenance File Upload </h3> <br> <p>File to Upload: <input type="file" name="filecsv" /></p> <p><input type="submit" name="Submit" value="Upload"></p>&nbsp +&nbsp&nbsp </body> </html> </form> __HTML__ } ## end sub PrintPage
  • Comment on Re: Need to use data passed from FORM from HTML page to CGI upload script. (cgi101)
  • Download Code

Replies are listed 'Best First'.
Re^2: Need to use data passed from FORM from HTML page to CGI upload script. (cgi101)
by coolsaurabh (Acolyte) on Jul 29, 2019 at 06:04 UTC

    Thanks for this script. Upload function is working absolutely fine but however Download button is not available on the page.There should be Download button which should download the requested file based on the Circle and Techno selection. part of your working code

    sub MaintenancePage { q{<!DOCTYPE html> <html> <head> <title>MAINTENANCE PAGE</title> </head> <body> <form name = f1 action="https://10.XXX.XXX.X:16311/ibm/console/webtop/ +cgi-bin/download_csv.cgi" method = "POST"> <p style="margin-left:10em;font-size:40px">MAINTENANCE PAGE</p> <br> <p style="margin-left:16.5em;font-size:20px">Circle:<select name="Circ +le" > <option value="Gabon">Gabon</option> <option value="Tanzania">Tanzania</option> </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs +p;&nbsp;&nbsp; Technology : <select name="Techno" > <option value="Core">Core</option> <option value="RAN">RAN</option> </select> </p> <br> <input type="submit" style="margin-left:30.5em" value="Download" onc +lick="this.form.target='_blank';return true;"> &nbsp;&nbsp;&nbsp; <input type="submit" value="Upload" onclick="f1.action='https://10.XXX +.XXX.X:16311/ibm/console/webtop/cgi-bin/Maintenance_Framework.cgi'; +return true;"> </form> </body> </html> }; } ## end sub MaintenancePage

    Download scipt

    #!/usr/bin/perl #use strict; #use warnings; use CGI 'header'; use CGI::Carp qw(fatalsToBrowser); local ($buffer, @pairs, $pair, $name, $value, %FORM); $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $Circle = $FORM{Customer}; $Techno = $FORM{Techno}; $filename = $Circle.'_'.$Techno.'.csv'; my $files_location = "/opt/IBM/Maintenance/archive"; if (!$filename) { die "You must specify a file to download"; exit; } print header( -type => 'text/csv', -content_disposition => "attachment;filename=$filename", ); my $fileloc = "$files_location/$filename"; open my $fh, '<', $fileloc or Error('open', 'file', $!); print while <$fh>; close $fh or Error ('close', 'file' ); sub Error { die "@_"; }
      .. Download button is not available on the page

      It looks like is in the HTML

      <input type="submit" style="margin-left:30.5em" value="Download" onc lick="this.form.target='_blank';return true;">
      poj

      Hi,

      Its your code now :)

      Did you copy that %FORM stuff from use CGI or die; ? The lesson of that node is to use param()