www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to change content-types

  1. #1
    Join Date
    Mar 2004
    Location
    North Jersey
    Posts
    80

    How to change content-types

    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!

  2. #2
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Code:
    #!/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;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles