Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Don't ask to ask, just ask
 
PerlMonks  

Sending Multiple Content-Types

by Petras (Friar)
on Jun 20, 2003 at 14:14 UTC ( #267600=perlquestion: print w/ replies, xml ) Need Help??
Petras has asked for the wisdom of the Perl Monks concerning the following question:

Ang Mga Monks!

I raped this code from BBQ to solve this problem (with thanks to Skeeve, benn and fglock; and yes, dear Skeeve, I learned a whole lot in the process!) I'm still a tried and true newbie for sure, but this was a great learning exercise (with one of the best lessons being How To RTFM. And like all good learning exercises (pmdev, I need a spell checker here!) it's left me with insite into great new things that I should learn. So I've been looking through the POD for CGI and can't find an answer to this question:

This code will send a generated file STDOUT:
# code ripped from perlmonks.org/index.pl?node_id=9277 chdir($filepath) || die "Unable to chdir to filepath"; my $filesize = -s $filename; # print full header print "Content-disposition: attatchment; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/x-tar\n\n"; # open in binmode open(READ,$filename) || die; binmode READ; # stream it out binmode STDOUT; while (<READ>) { print; } close(READ);
The following code will print an HTML "just in case" page:
my $q = CGI -> new(); print $q -> header( "text/html" ), $q -> start_html (-title => "File Download", -bgcolor => "#ffffcc" ), $q -> p ( "If the file does not begin to dowload automatically i +t can be downloaded <a href=\"$filepath$filename\">here</a>." ), $q -> p ( "<a href=../foo.html>Back to foo</a>"), $q -> p ( "<a href=../index.html>Bar Home</a>"), $q -> end_html;
So the question finally is: How can a script be written to send a file and print an HTML page (or vice versa), or more basically, can a single script send two types of Content-Types or headers?

Cheers!
-p
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

Comment on Sending Multiple Content-Types
Select or Download Code
Re: Sending Multiple Content-Types
by aquarium (Curate) on Jun 20, 2003 at 14:28 UTC
    perldoc cgi
    look for section starting Server Push
    you'll be creating multipart headers
    have fun
      Thanks for the reference, I'll give it a look again. But I'm thinking the POD said CGI push only worked with Netscape.

      you'll be creating multipart headers
      have fun

      That sound's nice and foreboding!
      Cheers,
      p
      Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

      -Howard Aiken
Re: Sending Multiple Content-Types
by fglock (Vicar) on Jun 20, 2003 at 14:38 UTC

    Include this line in the header of the html page:

    Refresh: 0;URL=/files/myfile.tar

    You still might need a "click here if the download doesn't start" link (just in case).

      fglock, What I'm really trying to figure out is how to put both together. I can use
      print "Content-disposition: attatchment; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/x-tar\n\n";
      as a header for the file (I want to send the file once the user clicks a submit button). I could also use a standard <code>content-type: text/html<code> to send an html file with an anchor link to the file.

      -but-

      If I send the file then try to print an HTML page I get an error, and if I try to print the HTML page then send the file the file's header get rendered as HTML. Multi-Part headers were suggested, I'm just having trouble figuring those out.
      Cheers,
      Petras
      Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

      -Howard Aiken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-04-18 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls