如何实现Content-Disposition:附件?
我试图让我的网站上的 mp3 通过左键单击下载,而不必右键单击并另存为,因此为了做到这一点,我必须设置 Content-Disposition: 附件。这是我的第一个网站,所以我不知道如何实际执行此操作,但是我是在 html 标记中执行此操作还是在我的托管网站上以某种方式设置此操作?
这是我的标记的示例。
<div class="download">
<a href="MP3/Morgan Page, Sultan & Ned Shepard, and BT feat. Angela McCluskey.mp3"
<img src="img/dlicon.png"/></a>
</div>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MP3 列表示例:
download.php :
Example of MP3 Lists:
download.php :
正如其他人所说,您不会在 HTML 中执行此操作,并且动态解决方案(例如,使用 PHP)是多余的。
对于您的情况,我将在 Web 服务器配置中配置 Content-Disposition 标头。对于 Apache,您可以基于位置设置标头,或者使用 .htaccess 与某些文件名匹配的文件。
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.
PHP 中也有一个特殊的函数:
bool http_send_content_disposition ( string $filename [, bool $inline = false ] )
请参阅此处的 PHP 手册: https://www.php.net/manual/en/function.http-send-content-disposition.php
There's a special function for that in PHP, too:
bool http_send_content_disposition ( string $filename [, bool $inline = false ] )
See PHP Manual here: https://www.php.net/manual/en/function.http-send-content-disposition.php