Re: Send a file

[email protected] ("Jay Savage")
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>
On Feb 19, 2008 7:15 AM, Beginner <[email protected]> wrote:
> Hi,
>
> I am trying to send a file in response to a request but am having
> trouble.
>
> Here's what I have so far. I am not certain what the headers should
> be. I am not getting any more info from my error_log except Premature
> end of script headers.
>
>
> my $s = (stat($file))[7];
>
> my $filename = $user.'.csv';
> seek $fh,0,0;
> print header(-type=>"text/tab-separated-values",-Content_length=>$s,
>         -Content_Disposition=>"attachment;filename=$filename");
> print $fh;
> close($fh);
>
>
> Can anyone offer some advice?
> Thanx,
> Dp.
>

Dermot,

I see a couple of things. First, the correct type should be
application/x-download or application/octet-stream" The file may
*contain* text, but you want the browser to treat the *container* as a
stream, not parse it.

Second, assuming you're following that normal convention of using $fh
to refer to a file handle, "print $fh" will print *to* the filehandle,
not read *from* the filehandle and print the contents to standard
output. See perldoc -f print for more detail.

You probably want something like

    print while (<$fh>);

You could also store the file to an array first, or "slurp" the file,
if you want to eliminate the loop.

HTH,

--jay
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.