Eugene Gluhotorenko
Eugene Gluhotorenko

Reputation: 3164

Show "Save As" dialog instead of playing audio file with JavaScript

How I can force browser to show the "Save As" dialog instead of playing an audio file when a user clicks the anchor? I need that for a Chrome extension I am developing.

Upvotes: 2

Views: 1104

Answers (2)

nullability
nullability

Reputation: 10675

The server delivering the audio file needs to provide the correct Content-Disposition HTTP header to trigger displaying the "Save As" dialog reliably on all browsers. If you were using PHP, you would do it like this:

header("Content-Disposition: attachment; filename=$filename");

There's no way to do it with Javascript alone as this involves an interaction between the server and the browser.

Upvotes: 5

Ian
Ian

Reputation: 50905

Supported in HTML5 by some browsers, you can use the download attribute of an <a>:

<a href="URL" download="new_filename.extension">Download</a>

Compatibility: http://caniuse.com/download

Reference: https://developer.mozilla.org/en-US/docs/HTML/Element/a#attr-download

Otherwise, you have to set it up on the server.

Upvotes: 3

Related Questions