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

How can I make a link to a Word document so that when clicked it downloads the document (rather than open it in a Word plugin session)? (Is there a cool Perl way to do this?)

Replies are listed 'Best First'.
Re: Forcing a Word Document to Download
by Abigail-II (Bishop) on Jul 11, 2003 at 01:02 UTC
    Two things: 1) It's not a Perl issue. 2) What happens at the client side is up to the client. That's how the Web was designed. You tell the client it's a Word document, and the client (whether or not directly directed by the user) decides whether to display it in a plugin, display it using an external program, save it to disk, send it to the printer, reject it or whatever it fancies to do. Why should the server care?

    Abigail

Re: Forcing a Word Document to Download
by sauoq (Abbot) on Jul 11, 2003 at 01:11 UTC
    Is there a cool Perl way to do this?

    You could write a CGI script something like this:

    #!/usr/bin/perl -w use strict; my $real_doc = 'path/to/actual/document.doc'; open WORD, '<', $real_doc or die "$real_doc: $!\n"; print "Content-type: application/octet-stream\n\n"; print while <WORD>;
    I don't know how "cool" that is... You might want to name it "word.doc" so that the browser tries to save it with a useful name.

    More seriously, The Right Way To Do It™ (for some notion of "right") is to change the mime-type in your server configuration. Of course, what's actually done with the data is up to the browser anyway.

    Why do you want to do this? If you want to provide separate links for "read it online" and "download it" you might want to consider just zipping the document up and providing a link to the zipped version for download.

    -sauoq
    "My two cents aren't worth a dime.";
    

      I don't have a Windows box handy to test with, but I'm fairly certain this won't work. When Windows/IE sees that it's got a generic octet stream to work with (and sometimes even when it doesn't), it will use the filename extensions to determine the document type.

      For this to work, then, you would have to also change the filename to something other than .doc, package it into a ZIP file and have the user unzip it, or convince your users to use a browser other than Internet Explorer.

        I don't doubt for a second that you are right. That's why I included the caveat:

        "Of course, what's actually done with the data is up to the browser anyway."

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Forcing a Word Document to Download
by cees (Curate) on Jul 11, 2003 at 03:49 UTC

    You can use the content type 'application/force-download' to tell the browser to download and save instead of displaying it inline. It is probably easier to get your web server to alter the content-type on static files then it is to write a script to do it. With Apache you could do it in a .htaccess file with ForceType or AddType.

    But as is mentioned above, it is really up to the browser to decide what it wants to do with the data.

    - Cees

      There is also the "Content-Disposition" header. "Content-Disposition: attachment" will cause many browsers to download the file. "Content-Disposition" isn't part of the HTTP spec but is part of MIME and is supported by Mozilla and IE.
Re: Forcing a Word Document to Download
by Mr_Person (Hermit) on Jul 14, 2003 at 16:03 UTC
    I don't have a machine with Word on it handy to test, but I've had good luck with the following code to have the browser download the output of the CGI instead of just showing it.
    #!/usr/bin/perl use strict; use warnings; use CGI; my $cgi = CGI->new; my $dir = '/path/to/file'; my $file = 'file.doc'; open(FILE, "$dir/$file") or die "Error opening file $file: $!"; print $cgi->header( -type => 'application/octet-stream', -attachment => $file, ); while(<FILE>) { print; } close(FILE);