Kathy181249 has asked for the wisdom of the Perl Monks concerning the following question:

I have a .dat file in the cgi-bin that stores data submitted on a form. Is there a way to have a link on an html page that will display the contents of the .dat file in plain text?

Replies are listed 'Best First'.
Re: Viewing .dat files
by Fletch (Bishop) on Oct 08, 2007 at 16:29 UTC

    Probably. Link to another program which reads the contents of the file in question and sends back whatever representation of said contents you desire.

    And a reading of How (Not) To Ask A Question probably couldn't hurt.

Re: Viewing .dat files
by bart (Canon) on Oct 08, 2007 at 16:33 UTC
    Just create a script that reads the file and converts it to plain text.

    The name ".dat" file tells me that the file format is not a standard format, but rather, a private format. So I cannot give you any details on how you must parse the file. You'll have to figure it out for yourself — or give more details.

Re: Viewing .dat files
by ikegami (Patriarch) on Oct 08, 2007 at 17:10 UTC
    Tell your web server the content type is text/plain. This could be done in an Apache .htaccess as follows:
    <Files *.dat> ForceType text/plain </Files>

    Untested.

      ikegami :
       ...
       ForceType text/plain
       ...
      
      Such solutions won't work if the document root
      is away from the script path. There won't be any
      allowed accesses into /cgi-bin/ without extra configuration
      (which you don't really want to do this way).

      You won't get around sth. in the lines of this: Re: Viewing .dat files

      Regards

      mwa

        If ScriptAlias causes problems with ForceType, a more sensible solution would be to put the .dat in another directory rather than breaking caching, HEAD requests, conditional requests, partial requests, etc by reimplementing the wheel.

        I particularly like how your script checks if a file exists but returns 200 OK either way.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Viewing .dat files
by throop (Chaplain) on Oct 08, 2007 at 17:29 UTC
    Gentle monk

    If you write your file with a .txt extension—rather than a .dat extension—then the browser will render it as text. No need to fool around with another program interpreting and reformatting it.

    Just link to it in the normal way:

    Here's my <a href="frob.txt"> data</a>!
    throop
Re: Viewing .dat files
by mwah (Hermit) on Oct 08, 2007 at 17:03 UTC
    Kathy181249s there a way to have a link on an html page that will display the contents of the .dat file in plain text?

    As the other answers suggest, you have to
    write a 'reader program' which is stored some-
    where and is accessible via /cgi-bin/ (in your case).

    The file could roughly look like:
    #!/usr/bin/perl # [plain] (no extension) # dump some /cgi-bin/ (or other) file in plaintext mode # 1) file must be in the same dir as script # 2) server has to provide PATH_INFO # 3) call with: http://my.server.org/cgi-bin/plain/textfile.dat # if 'plain' is the scripts name # my ($fn) = $ENV{SCRIPT_FILENAME} =~ /(.+?)[^\/]+$/g; $fn .= $1 if $ENV{PATH_INFO} =~ /([^\/]+)$/; print "Content-type: text/plain\n\n"; if( -f $fn ) { open my $fh, '<', $fn or warn "$!"; local $/; print <$fh> } else { print "($fn not found)\n" }
    As shown above, you name this snippet eg. "plain" and invoke it by
        http://my.server.org/cgi-bin/plain/textfile.dat
    (It uses the PATH_INFO Environment variable.)

    Remember to get the access and user rights correct
    when saving your stuff in /cgi-bin/

    Regards

    mwa