Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

CGI.pm woes with multipart attachments

by zentara (Archbishop)
on Jul 25, 2002 at 14:32 UTC ( [id://185204]=perlquestion: print w/replies, xml ) Need Help??

zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, recently I posted a snippet to allow multipart attachment
downloads. I generated my own headers and it works fine with
linux apache and Mozilla. Merlyn reponded, that I could also
do it with CGI.pm. Well at the time, I didn't have the latest version
of CGI.pm, which supported the sub multipart_final, so I just
shoved the script to my code collection. I recently upgraded to
the latest CGI.pm and tried Merlyn's script again, and it just
refuses to run for me. I am asking for "higher guidance" now.
My working code follows
#!/usr/bin/perl use warnings; use strict; my $boundary_string = "\n" . "--End" . "\n"; my $end_of_data = "\n" . "--End--" . "\n"; my @file_list = ("test.tgz","test1.tgz","test2.tgz"); print<<EOH; Content-type: multipart/x-mixed-replace\;boundary=End EOH foreach my $file (@file_list){ &amp;amp;send_file ($file); print $boundary_string; } print $end_of_data; exit(0); sub send_file { my $file = $_[0]; if (open (FILE, "< $file")) { print<<EOF; Content-type: application/octet-stream Content-Disposition: attachment\; filename=$file EOF binmode FILE; print <FILE>; close (FILE); }else{print "Cannot open file $file!"} }
############################################
merlyn's suggestion follows below:
#!/usr/bin/perl use CGI qw(:push); @ARGV = ("test.tgz","test1.tgz","test2.tgz"); $/ = undef; print multipart_init(); while (<>) { print multipart_start("application/octet-stream"); print $_; print multipart_end(); } print multipart_final();
########################################
When I run Merlyn's code, the filename is always
nph-download-multi, and I'm prompted to save that
filename 6 times instead of 3, and the name is always
nph-download-multi, instead of test.tgz, test1.tgz, etc.
Now I did try to add a "-filename=>$_" to the code to
get the filename to come thru, but to no avail. Also the resultant
download is 0 bytes. I've tried every combination of header
that I could think of, but no luck. So would anyone be kind
enough to post a multipart-download script , based on CGI.pm
which actually works, so I can see what I'm doing wrong?
Thanks

Replies are listed 'Best First'.
Re: CGI.pm woes with multipart attachments
by sedhed (Scribe) on Jul 25, 2002 at 21:53 UTC

    Perhaps a piece of the puzzle: Your magic line Content-Disposition: attachment\; filename=$file is what suggests the filename for the browser. CGI's multipart_start() function does not print this. However, you should be able to pass it yourself:

    print multipart_start( -TYPE=>'application/octet-stream', '-Content-Disposition'=>"attachment\; filename=test.tgz", # Have t +o quote the directive here );

    An aside: I don't know how, offhand, to determine the current filename using merlyn's code, other than a kludge like $filename = $ARGV[$count++]; since $_ is the contents of each file, not the file's name.

    Update: Sorry if this is obvious: Don't forget to try running it from a command-line to debug. That will let you see the exact headers and file contents the script is printing.

    Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://185204]
Approved by Snuggle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found