<a href="style.css">Download our CSS</a>
当我在 Chrome 中点击它时,它会在浏览器中打开 CSS。如何强制文件点击下载?
最佳答案
要使浏览器强制下载文件,需要特殊的 HTTP 响应 header 。因此,例如,如果您使用的是 PHP,一个简单的示例就是这样 -
<?php
// We'll be outputting a CSS
header('Content-type: text/css');
// It will be called style.css
header('Content-Disposition: attachment; filename="style.css"');
// The CSS is in style.css
readfile('style.css');
?>
无论您使用哪种后端语言(PHP/Ruby/Python/Node.js),只需在响应中发送那些特定的 HTTP header 即可。
关于javascript - 使用 jQuery 下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051556/