只有一个简单的html和js文件,我如何强制链接到MP3下载而不是只是播放?

时间:2022-10-22 14:54:11

i have a link to a media file such as an mp3, and i want it to get downloaded when the user clicks on it, instead of just having the file get played. the page i have in mind is just a plain static html page.

我有一个媒体文件的链接,如MP3,我希望它在用户点击它时下载,而不是只是播放文件。我想到的页面只是一个简单的静态html页面。

any ideas?

8 个解决方案

#1


5  

In order to make that happen you need to send a header with Content-disposition: attachment; filename=fname.ext header in your favorite language before sending the file.

为了实现这一目标,您需要发送带有Content-disposition:attachment的标头;在发送文件之前,您喜欢的语言中的filename = fname.ext标头。

Without knowing the specifics such as what language and what control you have over your server configuration I cannot give you more detailed instructions. You cannot do it with just html and javascript however.

如果不知道具体内容,例如您对服务器配置的语言和控制,我就无法向您提供更详细的说明。但是你不能用html和javascript来做。

#2


3  

Is far is I know that's not possible. Without the ability to set the appropriate headers the browser will decide what to do with the file, which usually is playback, you will have to ask to users to press, right-click+save as. If you have access to the server it is quite simple to set the headers in php or apache using .htacces

我知道这是不可能的。如果没有设置相应标题的能力,浏览器​​将决定如何处理文件(通常是播放),您必须要求用户按,右键单击+另存为。如果您可以访问服务器,使用.htacces在php或apache中设置标头非常简单

<FilesMatch "\.(?i:mp3)$">
  ForceType audio/mpeg
  Header set Content-Disposition attachment
</FilesMatch>

Or so the browser won't recognize it's an MP3 and won't even try opening it:

或者说浏览器不会识别它是MP3,甚至不会尝试打开它:

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

For php header setting see: http://nl.php.net/header

对于php标头设置,请参阅:http://nl.php.net/header

#3


0  

Agree with smazurov, and ultimately you cannot control the presentation of your media although the Content-disposition may get it done for you. Here is the RFC on the header in question.

同意smazurov,最终你无法控制媒体的演示,尽管内容处理可能会为你完成。这是有问题标题上的RFC。

#4


0  

Client side JavaScript can't achieve that (the nearest functionality is document.execCommand('SaveAs'), wich just saves the entire html page).

客户端JavaScript无法实现(最近的功能是document.execCommand('SaveAs'),它只保存整个html页面)。

#5


0  

Using google i found one thread with a js. used to save files. Check it out (posted in 2003, didn't test it myself).

使用谷歌我找到了一个带有js的线程。用来保存文件。检查出来(2003年发布,没有自己测试)。

The best soluting would be to use php, asp, or whatever language your server support, to fix this ;-)

最好的解决方案是使用php,asp或服务器支持的任何语言来解决这个问题;-)

#6


0  

<a href="linkto.mp3" download>Download MP3 File </a>

#7


0  

If you want them to be forced to download it from a link, then add the download attribute to the link element (filename optional):

如果您希望强制它们从链接下载,则将download属性添加到link元素(filename可选):

<a href="file.mp3" download="Song_Name.mp3">Download</a>

If you want users to be forced to download the file when they visit the URL by any means, you are out of luck. That is not possible with just HTML and JS. You have to have access to the server to affect the headers of the file before it gets loaded in the browser, as others have said.

如果您希望用户在以任何方式访问URL时*下载文件,那么您就不走运了。仅使用HTML和JS是不可能的。正如其他人所说,你必须有权访问服务器,以便在文件加载到浏览器之前影响文件的标题。

#8


-1  

While this is not a solution, change the file extension to anything other than MP3. And, ask the user to rename it once downloaded.

虽然这不是解决方案,但请将文件扩展名更改为MP3以外的任何其他内容。并且,要求用户在下载后重命名。

#1


5  

In order to make that happen you need to send a header with Content-disposition: attachment; filename=fname.ext header in your favorite language before sending the file.

为了实现这一目标,您需要发送带有Content-disposition:attachment的标头;在发送文件之前,您喜欢的语言中的filename = fname.ext标头。

Without knowing the specifics such as what language and what control you have over your server configuration I cannot give you more detailed instructions. You cannot do it with just html and javascript however.

如果不知道具体内容,例如您对服务器配置的语言和控制,我就无法向您提供更详细的说明。但是你不能用html和javascript来做。

#2


3  

Is far is I know that's not possible. Without the ability to set the appropriate headers the browser will decide what to do with the file, which usually is playback, you will have to ask to users to press, right-click+save as. If you have access to the server it is quite simple to set the headers in php or apache using .htacces

我知道这是不可能的。如果没有设置相应标题的能力,浏览器​​将决定如何处理文件(通常是播放),您必须要求用户按,右键单击+另存为。如果您可以访问服务器,使用.htacces在php或apache中设置标头非常简单

<FilesMatch "\.(?i:mp3)$">
  ForceType audio/mpeg
  Header set Content-Disposition attachment
</FilesMatch>

Or so the browser won't recognize it's an MP3 and won't even try opening it:

或者说浏览器不会识别它是MP3,甚至不会尝试打开它:

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

For php header setting see: http://nl.php.net/header

对于php标头设置,请参阅:http://nl.php.net/header

#3


0  

Agree with smazurov, and ultimately you cannot control the presentation of your media although the Content-disposition may get it done for you. Here is the RFC on the header in question.

同意smazurov,最终你无法控制媒体的演示,尽管内容处理可能会为你完成。这是有问题标题上的RFC。

#4


0  

Client side JavaScript can't achieve that (the nearest functionality is document.execCommand('SaveAs'), wich just saves the entire html page).

客户端JavaScript无法实现(最近的功能是document.execCommand('SaveAs'),它只保存整个html页面)。

#5


0  

Using google i found one thread with a js. used to save files. Check it out (posted in 2003, didn't test it myself).

使用谷歌我找到了一个带有js的线程。用来保存文件。检查出来(2003年发布,没有自己测试)。

The best soluting would be to use php, asp, or whatever language your server support, to fix this ;-)

最好的解决方案是使用php,asp或服务器支持的任何语言来解决这个问题;-)

#6


0  

<a href="linkto.mp3" download>Download MP3 File </a>

#7


0  

If you want them to be forced to download it from a link, then add the download attribute to the link element (filename optional):

如果您希望强制它们从链接下载,则将download属性添加到link元素(filename可选):

<a href="file.mp3" download="Song_Name.mp3">Download</a>

If you want users to be forced to download the file when they visit the URL by any means, you are out of luck. That is not possible with just HTML and JS. You have to have access to the server to affect the headers of the file before it gets loaded in the browser, as others have said.

如果您希望用户在以任何方式访问URL时*下载文件,那么您就不走运了。仅使用HTML和JS是不可能的。正如其他人所说,你必须有权访问服务器,以便在文件加载到浏览器之前影响文件的标题。

#8


-1  

While this is not a solution, change the file extension to anything other than MP3. And, ask the user to rename it once downloaded.

虽然这不是解决方案,但请将文件扩展名更改为MP3以外的任何其他内容。并且,要求用户在下载后重命名。