Make left-click cause an audio file download instead of stream?

R

Robert Oschler

Hello,

I have a web site that has an audio file on it that I make available for
download. Right now I'm using the "right-click/save-as" approach. This is
because left-clicking the link to the audio file initiates client-side
streaming/playing of the audio file. Is there a way with Javascript to make
a left-click trigger a download of the file instead?

BTW, I don't want to have convert the mp3 file to winzip to make this
happen, because some people don't have an unzip utility.

thx
 
F

Fabian

Robert Oschler hu kiteb:
Hello,

I have a web site that has an audio file on it that I make available
for download. Right now I'm using the "right-click/save-as"
approach. This is because left-clicking the link to the audio file
initiates client-side streaming/playing of the audio file. Is there
a way with Javascript to make a left-click trigger a download of the
file instead?

BTW, I don't want to have convert the mp3 file to winzip to make this
happen, because some people don't have an unzip utility.

Javascript can't force a browser to handle a file format in a particular
manner. That is usually left to the default settings of the browser,
modified by whatever applications or user settings have been changed.

I guess if you rename teh file extension as .somethingoffbeat then the
default action would be to ask to download, but that seems kind of
awkward.
 
M

Michael Winter

I have a web site that has an audio file on it that I make available for
download. Right now I'm using the "right-click/save-as" approach. This
is because left-clicking the link to the audio file initiates client-
side streaming/playing of the audio file. Is there a way with
Javascript to make a left-click trigger a download of the file instead?

No[1].

If you can use a server-side script, you can send the file with the
application/octet-stream content type, which will force the "Save as"
dialog. However, I don't know the specifics, so you'll have to ask in a
newsgroup dedicated to that language.

Mike

[1] Even if there was, it wouldn't be compatible with all browsers.
 
G

Grant Wagner

Stephen said:
Michael said:
[...] left-clicking the link to the audio file initiates
client-
side streaming/playing of the audio file. Is there a way with
Javascript to make a left-click trigger a download of the file instead?


No[1].

If you can use a server-side script, you can send the file with the
application/octet-stream content type, which will force the "Save as"
dialog. [...]

And, AIUI, even this might not work in some browsers because those
browsers ignore the Content-type header and use the file's extension to
determine how to treat it. And it appears some browsers can be
configured to use either content-type or file extension.

Stephen

Adding the following headers from the server (as an example) seems to work
reliably in all browsers I've tested:

Content-Disposition: attachment; filename="file.xls"
Content-type: application/vnd.ms-excel

Even IE on a Windows system with Excel installed prompts to download the file,
rather then loading it in-place in IE.

Adding the following headers from the server ensures most browser will force a
download of it:

Content-Disposition: attachment; filename="file.mp3"
Content-type: application/octet-stream

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top