in reply to Save results on web page to doc or txt

Well ... you don't say whether the word doc will look different (graphics, fonts sizes, etc) from the TXT doc so I'll assume they dont.

Just generate the plain text page and then set the content-type (CGI header method) to text/plain or application/ms-word. MS-Word will open and display the plain text just fine.

The only caveat being that some browsers (and we all know there really doesn't need to be an s on the end of browsers) totally ignores the content-type so you may have to also changed the extension of the file being delivered (.txt or .doc). There are ways to do this (mod-perl, re-directs, etc) but get the content-type approach working first and then jump the hoops only if you have to).

-derby

Update: I'm also assuming, you don't want the user to just use the File->Save As function of the browser after your results page is delivered to the browser - that would really be the easiest approach.

  • Comment on Re: Save results on web page to doc or txt

Replies are listed 'Best First'.
Re^2: Save results on web page to doc or txt
by Wampa (Hermit) on Jul 13, 2004 at 12:11 UTC

    You assume very well that I dont want usre to use File->Save As because it is very hard operation for many people who use IE . I want do it comfortably for users. I will place on page anchor which run script which save document on user comp. Now I see it will be little complicated as think.

    Programing in Embperl and all questions are related to Embperl.
    Excuse my bad English !!!

      I don't think it should be that difficult. Here's a snippet from one of my scripts that I use for serving up files but it shouldn't take too much to extract what you need from it.

      #!/usr/bin/perl -wT use strict; use CGI; $CGI::HEADERS_ONCE = 1; use constant BUF_SIZE => 4096; # Unbuffer the output stream: $| = 1; # Set the environment for Taint mode: BEGIN { $ENV{PATH} = "/bin:/usr/bin"; delete @ENV{qw (IFS CDPATH ENV BASH_ENV)}; } my $q = new CGI; my $source = &get_file_name($q); # IE is funky, doesn't seem to understand # "-type => 'text/octet-stream'", # wants a .whatever filename my $save_as_name = &get_saveas_name($source); if (open(IN, $source)) { print $q->header( -type => 'text/octet-stream', -attachment => $save_as_name ); my $buffer; binmode STDOUT; while (read(IN, $buffer, BUF_SIZE)) { print $buffer; } close IN; } else { print &get_error_page($q, $save_as_name, $!); } ...

        Thanks. I try use your code but I must translate your code to embperl before.

        Programing in Embperl and all questions are related to Embperl.
        Excuse my bad English !!!