How to implement Content-Disposition: attachment?

ぐ巨炮叔叔 提交于 2019-11-26 08:35:01

问题


I am trying to make it so that mp3\'s on my site are downloaded by left clicking instead of having to right click and save as, So in order to do that, I have to set the Content-Disposition: attachment. This is my first website so I am new to how to actually do this, but do I do this in my html markup or do I set this somehow with my hosting site?

Here is an example of what my markup looks like.

<div class=\"download\">
<a href=\"MP3/Morgan Page, Sultan & Ned Shepard, and BT feat. Angela McCluskey.mp3\" 
<img src=\"img/dlicon.png\"/></a>
</div>

回答1:


Example of MP3 Lists:

<a href="download.php?file=testing.mp3">Download MP3</a>
<a href="download.php?file=testing2.mp3">Download MP3</a>

download.php :

<?php

$file = $_GET['file'];  

header('Content-type: audio/mpeg');

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

?>



回答2:


As others have said, you don't do that in HTML, and a dynamic solution (e.g., using PHP) is overkill.

In your case, I'd configure the Content-Disposition header in the web server config. For Apache, you could set the header based on location, or have a .htaccess file that matches certain filenames.




回答3:


There's a special function for that in PHP, too:

bool http_send_content_disposition ( string $filename [, bool $inline = false ] )

See PHP Manual here: http://de2.php.net/manual/en/function.http-send-content-disposition.php



来源:https://stackoverflow.com/questions/8875949/how-to-implement-content-disposition-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!