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

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

4

1 回答 1

5

Special HTTP response headers are required to make the browser force download a file. So for example if you're using PHP a simple example would be this -

<?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');
?>

It doesn't matter what backend language (PHP/Ruby/Python/Node.js) you're using, just send those specific HTTP headers in the response.

于 2013-04-17T04:34:58.870 回答