Forum Moderators: open

Message Too Old, No Replies

MIME headers in HTML pages

forcing a save dialog box on a download link

         

usr_c

9:27 pm on May 9, 2004 (gmt 0)

10+ Year Member



hi everyone

I have a question that I hope someone can help me with. I've done some research on Content-Disposition and Content-Type in a HTML file's header to force downloading files like PDF's instead of automatically displaying them in the browser but I'm a but confused as to how I should have them placed in my HTML file.

I've currently got the following, which doesn't work:

<META HTTP-EQUIV="Content-Type" CONTENT="application/octet-stream">
<META HTTP-EQUIV="Content-Disposition" CONTENT="attachment; filename=somefile.pdf">

Wouldn't that set the content-type of the page instead of just the file? The RFC2183 documentation says to use the two attributes I posted, but doesn't specify how they should look within a HTML page.

So I guess what I'm asking is whether it is possible to override a server's MIME settings and force the download of a file of a certain extension without having to use something like PHP?

thanks a lot.

DrDoc

9:50 pm on May 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Those are not MIME headers, but merely meta information. The headers must be sent by the server, before the actual page is sent to the browser. Once the meta tag is read by the browser, it is already too late.

You have two options:

1) make changes to your Web server to modify the headers for the particular page
2) use a server side scripting language (such as PHP/Perl/ASP) which can then set the headers you need

usr_c

10:49 pm on May 9, 2004 (gmt 0)

10+ Year Member



thanks for the welcome DrDoc. That explains the php errors being logged about "headers already sent".

but if I were to use something like this

<?
$files="info.zip";
$filenames="info.zip";
$url= "/www/download/";
$total=$url . $files;
Header ( "Content-Type: application/octet-stream");
Header ( "Content-Length: ".filesize($total));
Header( "Content-Disposition: attachment; filename=$filenames");
readfile($total);
?>

1. would I include it at the top of my html page?
2. does the content-type apply to all downloads on that page and does it apply the "attachment" part to them, thus forcing a save dialog screen to show up?

thanks

[edited by: tedster at 2:05 am (utc) on May 10, 2004]
[edit reason] fix side-scroll [/edit]

vkaryl

1:38 am on May 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can usually force download of a pdf file simply by zipping it first. Yes, that does require the extra step for the user of unzipping it once downloaded, but that shouldn't be too big a deal.

You might even consider using winzip or similar to make a self-extracting .exe out of it - then all the end-user has to do is double-click on it....

usr_c

12:08 pm on May 11, 2004 (gmt 0)

10+ Year Member



thanks, but if I were to following the PHP script route, would I include it at the top of my HTML page?

DrDoc

2:36 pm on May 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you should include that at the top of the page, before anything else is sent back to the browser