views:

239

answers:

4

I need a link in an HTML page that could use any JavaScript to "start downloading" a normal webpage / file that is on my webserver, either filetypes like PDF / ZIP or simply the outputs of PHP / ASP / HTML.

I know this is possible with some server-side download script that returns a *mime_content_type* so the browser handles it based on its configuration. (typically Download) but I would really like a JavaScript-only solution. Any ideas?

+3  A: 

You simply cannot do that in javascript, as you need to change the mime type header. The best you could do is to use open('the url') to open it in another window, but I don't think that fits to your needs and could be blocked by popup blockers.

Fabien Ménager
IE browsers could do that with an execCommand() call
Jenko
If you target only IE users, you could use a method like this, but I would not recommend it !
Fabien Ménager
A: 

You will need to serve the file using the webserver as Fabien points out. For example with ASP you would give the file a content type before serving the file:

Response.contenttype = "application/octet-stream"
Michiel
+1  A: 

If you want to force the browser to display the Save As dialog I uses the following headers:

Content-type: application/octet-stream
Content-Transfer-Encoding: binary
Content-disposition: attachment; filename=test.pdf
WizKid
Hate to downvote, but this is ignoring the question. He asked for a javascript-only solution. To change the response headers, you need something server side.
Macha
+1  A: 

That has to be done server-side. To do it on an Apache Server, add this to your .htaccess file: AddType application/octet-stream mp3 where mp3 is the filetype that needs to be downloaded.

Kerrick