Hello Monks, I would like some advice on creating a multipart form that, at one part, downloads a csv file. When I run this, the contents of the csv file are displayed on the webpage, but there is no option to download it. I am trying to get a dialog box to enable the file download. I have managed this when the form is not multipart, but can't get it to work in this example. In addition to the code below, I have tried using CGI (not Safe) and I've used a redirect to a separate cgi script where a download box works. I am fairly new to Perl so I am open to suggestions for alternative ways to achieve this.
The code for a stripped-down example is below.
Thanks in advance!

#!/usr/bin/perl -w use strict; use CGI::Safe qw/:standard taint /; # Initial variables my @languages = ('English', 'Francais'); my @colours = ('Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', ' +Violet', 'Black'); my $fileDir = "/home/stef/outputs/"; my $filename = "test.csv"; ################################################################### # Basic Form Config # ################################################################### my $query = new CGI; print $query->header; print $query->start_html("Test Basic Form"); my $step = param('step') || 0; # Subroutines: part1() unless $step; part2() if $step == 2; part3() if $step == 3; print $query->end_html; ################################################################### # Subroutines # ################################################################### sub part1 { print $query->h2("Welcome to this basic form"); print $query->startform; print $query->hidden({-name => 'step', -value => 2, -override => 1}); print $query->popup_menu({-name => 'language', -values => \@languages, -default => \$languages[0], -label => "Please select language"}); print $query->submit({-value => "Continue"}); print $query->endform; } sub part2 { print $query->h2("Part Two"); print $query->startform; print $query->hidden({-name => 'step', -value => 3, -override => 1}); print $query->hidden({-name => 'language'}); print "<p>Please choose your favourite colour: </p>"; print $query->radio_group({-name => 'colour', -values => \@colours, -default => \$colours[0], -linebreak => 'true'}); print $query->submit({-value => "Continue"}); print $query->endform; } sub part3 { print $query->h2("Almost Done!"); print $query->startform; print $query->hidden({-name => 'step', -value => 4, -override => 1}); print $query->hidden({-name => 'language'}); print $query->hidden({-name => 'colour'}); print "<p>Press the Submit button to download csv file</p>"; print $query->submit({-value => "Continue"}); print $query->endform; download($filename) or error('An unknown error has occured.'); } sub download { my $file = $_[0] or return(0); my $path_to_files = "/home/stef/outputs/"; open(my $DLFILE, '<', "$path_to_files/$file") or return(0); print $query->header(-type => 'application/x-download', -attachment => $file, 'Content-length' => -s "$path_to_files/$file", ); binmode $DLFILE; print while <$DLFILE>; undef ($DLFILE); return(1); }

In reply to CGI - Creating Multipart Form with a File Download by stefl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.