in reply to Re: CGI outputs plain text -- can't "Save Page As?"
in thread CGI outputs plain text -- can't "Save Page As?"

Hmmm...I'm not completely understanding your post, so I'll just post all my code here. I suppose it might be possible to do this all using a single CGI script, but I found it easier just to implement it as two separate scripts, so I didn't have to detect what state I should be in. The way I'm handing the transaction to the second script is through a form with a file upload in it. Do you get the output on the screen from the second script?

convert_word1.cgi:

#!/usr/bin/perl use strict; use CGI; use Digest::SHA1; use URI::Escape; use Tetragon; my $q = new CGI; print Tetragon::html_header(); print <<STUFF; <h2>Converting an MS Word File</h2> <p> This web-based program is designed to convert a fiction manuscript fro +m MS Word format to plain text format. The main advantage of this over a simple "Save a +s" in Word is that it will automatically format italics <tt>_like this_</tt> and bol +dface <tt>*like this*</tt>. Once you upload your file, the resulting convert +ed version will be displayed in your browser, and you can cut and paste i +t into a text editor such as NotePad. </p> <p>Caveats:</p> <ul> <li>This is a fairly computation-intensive thing for my server to do, +so I don't want this to be a service that's used heavily by a huge number of people. Pl +ease don't use it from an automated script, and don't use it more than a few +times per day.</li> <li>Don't assume blindly that the results are what you want. Look over + the output file carefully, and fix any problems by hand.</li> <li>This probably will not work with the OOXML format used as the defa +ult format by the most recent versions of word. To make sure that's not a problem, do a "Save as +" in Word, and choose an older format.</li> </ul> STUFF print <<STUFF; <form method="post" action="http://tetragonsf.com/cgi-bin/convert_word +2.cgi" enctype="multipart/form-data"> <p>file: <input type="file" name="foo" size="40"/></p> <p> <input type="submit" name="Continue" value="Continue" /> < +/p> </form> STUFF print Tetragon::html_footer();

convert_word2.cgi:

#!/usr/bin/perl use strict; use CGI; use Digest::SHA1; use Tetragon; use IO::File; use POSIX; use constant UPLOAD_DIR => "/usr/local/www/tetragonsf/uploads"; # not +clear to me that this actually has any effect use constant BUFFER_SIZE => 16_384; use constant MAX_FILE_SIZE => 2_000_000; use constant MAX_DIR_SIZE => 500_000_000; use constant MAX_OPEN_TRIES => 100; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = MAX_FILE_SIZE; my $q = new CGI; print <<'HEADER'; Content-type: text/plain HEADER my $filename = $q->param('foo'); my $fh = $q->upload('foo'); my $buffer = ''; my $t = ''; binmode $fh; while (read($fh,$buffer,BUFFER_SIZE)) { $t = $t . $buffer; } my $in; do {$in = POSIX::tmpnam()} until ! -e $in; my $out; do {$out = POSIX::tmpnam()} until ! -e $out; open(F,">$in") or die "error opening temporary file for doc"; binmode F; print F $t; close F; system("/home/bcrowell/Documents/programming/scripts/fiction_word_to_t +xt <$in >$out")==0 or die "error executing fiction_word_to_txt"; local $/; # slurp whole file open(F,"<$out") or die "error reading output of fiction_word_to_txt"; my $txt = <F>; close F; unlink $in; unlink $out; print $txt;

Replies are listed 'Best First'.
Re^3: CGI outputs plain text -- can't "Save Page As?"
by Joost (Canon) on May 28, 2007 at 02:14 UTC
    Something weird is indeed going on. It appears that firefox (at least my firefox/iceweasel 2.0.0.3):

    1. does not re-upload a file on a reload (my mozilla seamonkey/iceape 1.0.8 DOES re-upload on reload).

    2. does a new GET request for the result page when you try to save it. (of course, that means you get a blank page - except that you're printing an extra blank line in convert_word2.cgi). (mozilla does the same).

    Here's the script I used to test that. Note that if you outcomment the "Content-Disposition" line, firefox will request you to save the output directly, which fixes the bug, in some sense.

    #!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new; my $method = $q->request_method; my $r = rand(1000000); my $fh = $q->upload("file"); warn "fh is ",($fh ? "" : "not "),"defined"; if ($fh) { #print "Content-Disposition: attachment; filename=output.txt\n"; print "Content-type: text/plain\n"; print "\n"; print "METHOD=$method\n"; my @data =<$fh>; warn "data = '",@data,"'"; print @data; exit; } print <<ENDHTML; Content-type: text/html <html> <body> METHOD=$method <form action="test.cgi?r=$r" method="POST" enctype="multipart/form-dat +a"> <input type="file" name="file"><input type="submit"> </form> </html> ENDHTML
      Interesting! I guess it should have occurred to me to try it in different browsers before posting here in the first place. Neither galeon nor konqueror showed the weird behavior that firefox did. In any case, the Content-Disposition thingie works great for my purposes, so I've just gone ahead and put it in. Thanks, Joost -- you are a truly kind and enlightened perl monk!