Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Sending non-html files to a browser through a script

by michellem (Friar)
on Aug 15, 2001 at 13:39 UTC ( [id://105039]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi folks,

This is probably in the realm of the dumb question, but here goes:

I wrote this content management system for a client, who needs all of their files protected (it's basically a restricted extranet). Originally all of the files were protected via apache's .htaccess protocol. Now, I've got a perl based login, and protection via a cookie that I've set, and read when the script is called. Most of the content is generated on the fly by the system with information in a back-end database. However, there are several kinds of files that I want to be able to show/give people access to, that are stumping me.

So these are the scenarios:
1) Regular content - deliverd up via script after cookie check.
2) Flat file html - delivered up via script, gotten from abolute path on the back-end (in an administratively protected area) - read through line by line and printed to the browser.

The ones that I don't know how to do: Images and pdf files, and any other binary file types. I can't send a URL out, because then the user would have to get into the administrative area - which they don't have a password for. How do I send a binary file to the browser from within a script?

Thanks!

  • Comment on Sending non-html files to a browser through a script

Replies are listed 'Best First'.
Re: Sending non-html files to a browser through a script
by TheoPetersen (Priest) on Aug 15, 2001 at 13:55 UTC
    This should just be a matter of setting the right content header and sending the bytes along. Make sure your script hasn't sent any other content type headers.
    my $pdf = slurp("file.pdf"); # Contains the whole PDF file print "Content-type: application/pdf\n\n$pdf";
Re: Sending non-html files to a browser through a script
by trantor (Chaplain) on Aug 15, 2001 at 14:03 UTC

    If I understood well, all you need to do sending a Content-Type header with the appropriate value (e.g. application/pdf for a PDF document) and then you can pump your data through the connection, it does not matter if yuo're copying an existing file or if you're generating it on the fly.

    Among the many sources of Content-Type values:

    • your Web server configuration;
    • this link.

    If you know in advance how many bytes you're sending, a Content-Length header is a good thing to output as well.

    You can set the Content-Type header in many ways, using the CGI module you can use for example the header method.

    Happy sending!

    -- TMTOWTDI

Re: Sending non-html files to a browser through a script
by tachyon (Chancellor) on Aug 15, 2001 at 14:07 UTC

    You will find a good answer here which includes how to get the browser to give the file the correct name (not the secript name) Re: download a file from non-web space. Have a look at the root node of this thread as well for some more links on this. Although perhaps not considered perfect form you can use application/octet-stream for any binary format. You could construct a hash that maps extensions to MIME type but why bother - the browsers often ignore it anyway and go by extension!

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105039]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.