Click to See Complete Forum and Search --> : How to change content-types


lindamoran
05-05-2005, 11:30 AM
I have a CGI that runs twice: the first time, it displays a form, then it calls itself to process the form. On the first time around, obviously I have to use content-type:text/html

But on the second time around, my visitor may have asked to download a file when he filled in the form. Now I need to change the content-type. Here is my code:
print "Content-type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder;

But it doesn't work because these print statements are interpreted to mean "print all this stuff to the screen."

How do I change the content-type in a Perl CGI?

Thanks!

Nedals
05-06-2005, 01:18 AM
#!/usr/bin/perl

use strict;

if ($ENV{'REQUEST_METHOD'} =~ /post/i) {
## Second time through
.. process ..

if (..return a file..) {
print "Content-type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder;

} else { ## return a different page
print "Content-Type:text/html\n\n";
..the HTML..
}
} else {
## First time through
print "Content-Type:text/html\n\n";
..the HTML..
}
exit;