Tony Frasketi wrote:
> 
>> What I was intending was to call the cgi script and rather than it
>> printing the normal text/html header it would print the header directly,
>> that way you are guaranteed to be operating the way you intended.
> 
> Hi Wiggins
> Thanks for this suggestion... I've tried the following bit of CGI script
> with three different file types (.txt, .cgi, .dat) and in each case *DO*
> get the 'Save as...' dialogue box to come up and when I select 'Save to
> disk' and click ok, It appears as if a transfer occurs but only a
> zero-length file is saved to my local hard disk directory.  The code is
> as follows...

Ok, so almost there.... let's back up a step and understand the whole
thing. The HTTProtocol is very similar to other common net protocols
where there is a header section and a data (or payload) section. The two
are separated by a blank line. So right now you are providing the header
(apparently correctly). The client reads that information and based on
what it knows how to handle it decides on a way to use the payload. In
this case we are giving the browser less than optimal descriptors of the
payload. Basically we are telling it that there is something coming and
that neither I nor you know what it is. So it does the only thing it
can, "Save as...". The only hint you are providing it a suggestion about
what to call that thing. So right now you are providing a header, but
not a payload, let's add it....

> ============================================================================
> 
> #!/usr/local/bin/perl -w
> 
> # testoctetstream_1.cgi
> 
> # My base directory and base directory URL
> my($basedir) = "<base directory>";
> my($baseurl) = "<base URL>";
> 
> # Location of the file to be downloaded
> $fileloc = "$basedir/cgi-bin/test/xxx.txt";
> #$fileloc = "$basedir/cgi-bin/test/testdu1.cgi";
> #$fileloc = "$baseurl/cgi-bin/test/testdu.dat";
> 
> # Name of file to be downloaded
> $filename = "xxx.txt";
> #$filename = "testdu1.cgi";
> #$filename = "testdu.dat";
> 
> # NOTE: Uncomment the desired $filename and $fileloc above
> 
> # Set The headers
> print "Content-Type: application/octet-stream;\n";
> print "Content-Disposition: attachment; filename=\"$filename\"\n";
> 
> # ---------------------------------------------------------------
> # Note: Can't figure out what to put in here to actually download
> # the darn file!!!
> # ---------------------------------------------------------------
> 

This is the simple part, and probably looks a little like.

open file...
read in file...
print file back to browser...
close file

There are simpler ways to do this, but what I have come to use looks like:

my $READHANDLE;
unless (open $READHANDLE, $file) {
    # error handling...
}
binmode $READHANDLE;

$| = 1;

while ( my $length = sysread $READHANDLE, my $buffer, $block_size ) {
    next unless defined $length;

    my $offset = 0;
    while ( $length ) {
        my $written  = syswrite STDOUT, $buffer, $length, $offset;
        $length     -= $written;
        $offset     += $written;
    }
}

close $READHANDLE;

> print "\n";

You will want to remove this as it will be an addition to the actual
data which can break some formats.

> exit;
> ============================================================================
> 
> The above cod was tested in both Mozilla and IE browers with the same
> results.
> 
> 
> It appears I'm missing some statement that should follow the
> Content-Disposition
> statement.
> 

Does this hit the mark?

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to