Frederik.L Frederik.L - 9 months ago 38
Javascript Question

Is it possible to initiate a download prompt in browser for recognized mime-types using only JavaScript (server-side approach)?

I would like to allow the user to directly download a file with a single click. There is however a problem when it comes to known meme-types like html, audio, video, etc. Ideally, I would like to trigger a download prompt for audio/video files. Ultimately, I would like to do it for html documents too. The main idea is to make it easy for users to download files without asking them to navigate into the context menu.

I think for example to people that are not really comfortable with a computer and his main functions. These people will surely prefer a better way than "save as".

The reason why I am looking for a JavaScript solution is that the PHP approach only works if you are in a web site context. Whenever you are inside a plugin or injected script context (i.e. developing a plugin for Firefox, Chrome or Safari), you may want to avoid asking for a server-side response.

I tried to achieve this with
. It does work, although it is glitchy and fails for huge files.

Then, I tried Downloadify which does not work in every situations.

Is there a pure javascript, no ajax way to trigger a download prompt so the user can directly download a file using a simple left click?

Any help is appreciated.

Answer Source

There is a new download attribute in HTML5 that you can annote links with. It indicates to the browser that the resource should be downloaded rather than navigated to. Right now, it only works in Chrome, but it is part of the HTML spec and will hopefully be adopted by other browser soon.

Demo: More info: