用jQuery下载链接

时间:2013-04-17 04:33:18

标签: javascript jquery

<a href="style.css">Download our CSS</a>

当我在Chrome中点击它时,它会在浏览器中打开CSS。如何强制下载文件?

1 个答案:

答案 0 :(得分:5)

需要特殊的HTTP响应标头才能使浏览器强制下载文件。因此,例如,如果你使用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标头。