Forum Moderators: phranque
I'm sure we've all seen sites where you can click on a button that says "Download" and the typical File Download text box appears offering the visitor the options of "Save" "Open" "Cancel".
Can anyone either tell me where to get this code and the graphic for the box or, better yet, can anyone supply me with this code and graphic?
Thanks so much!
This is done using a server-site language that "munges" the header. That is, when you click a link, the server sends a header to the browser telling it what type it is: "this is an html or text document" (content-type:text/html.) "This is a pdf" (content-type:application/pdf.) When you send an unrecognizable content-type, it has no choice but to prompt a download: content-type:bad/type or content-type: sdfsdfsds(gibberish.)
There's more to it than that, and many use octet-stream, but the bottom line is it's generally something you do server side. If you don't know or have access to that, a simple solution is to zip up the files, this will always prompt as open with/download dialogue box.
Better, for me, is to use PHP and Content-disposition.
Note: There is a difference in allowing the option to save a file, and forcing to save a file. Don't know if you need/want to take that into account.
It can also be done with .htaccess, though would not be my choice.
Would I need to contact my webhost? as this is a server side issue? Could they help me with this?
If I understand correctly, a zipped file prompts a "save"-type box automatically and I don't need to worry about server side or anything else?
PHP and Content-disposition - what is this; or is this a whole lesson in itself?
Thanks again.
Would I need to contact my webhost? as this is a server side issue? Could they help me with this?
No. They will have no interest. It is completely outside of their responsibilities. Of course, if you are coughing up Rackspace money, they would probably help you anyway. The server side options would be your responsibility and within your power to implement. If not - new host.
...............................
If I understand correctly, a zipped file prompts a "save"-type box automatically and I don't need to worry about server side or anything else?
Correct. No browser can extract and display files from a .zip, so the prompt is automatically generated. A .zip might have one file, or it might have multiple directories, files, and file types.
...............................
[PHP and Content-disposition - what is this; or is this a whole lesson in itself?
Head to the PHP forum for this. It is several lessons. There is more than one way and reason to do what you want.
...............................
If not in a hurry, I would wait before implementing, learn how to force certain files at certain times to force a download, or at least a download prompt option (where the user can open or download - their choice).
If it's a 'right now' thing, the .zip option will be fast and easy. You can go back later and 301 those URLs if switching your method after learning the other options.
You can force a download from any language by simply doing
print "content-type: bad-type\n\n";
print "this will download as an unknown file type";
However, browsers may sniff my content and say "no it i'dn't, it's plain text!" and display it in the browser anyway. This was just for an example.
There are other attributes that allow you to push out a pre-set file name, etc., but as mentioned, a .zip archive will work fine without server-side programming. Sounds like you're not ready for either of these:
PHP and Content-disposition - what is this; or is this a whole lesson in itself?
PHP is a server-side dynamic language.
HTTP headers, of which content-disposition is an optional header, are part of the HTTP protocol specification [w3.org]. Neither are restricted to PHP, other languages can generate them too, and they are inherently generated by a web server as well depending on the content being served.
Content-disposition headers [ietf.org]
the typical headers for your purposes should look like this example:
Content-type:application/octet-stream
Content-length:12345
Content-disposition:attachment; filename=example.pdf