Reputation: 113986
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?
Upvotes: 0
Views: 1019
Reputation: 7478
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.
Upvotes: 1
Reputation: 140205
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.
Upvotes: 3
Reputation: 4908
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
Upvotes: 1
Reputation: 2634
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"
Upvotes: 0